您的位置:首页 > 移动开发 > Android开发

Android webservice 入门 (1)—web中转站

2014-05-12 14:01 267 查看
Android webservice 入门 (1)—web中转站

 

   本文将介绍web服务器的搭建,采用.net技术。

 

新建项目 ASP.NET Web服务应用程序





在浏览器中运行Webservice1.asmx后,出现如下接口,Android就是通过这种接口访问数据库,进行数据传输



为了方便我们对数据库的操作,我们新建一个类。



然后,我们将数据库操作全面写进这个类里





然后,再webservice1.asmx中调用这个类



综上,简单的web服务应用已经完成,接下只需生成网页,开启服务器即可。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebServiceTest
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
DBOperation dbOperation = new DBOperation();

[WebMethod]
public string LoginByUser(string user, string pwd)
{
return dbOperation.LoginByUser(user, pwd);
}

}

}

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;

namespace WebServiceTest
{
public class DBOperation : IDisposable
{
//用于连接数据库
public static SqlConnection con = null;
//默认构造函数
public DBOperation()
{
if (con == null)
{
con = new SqlConnection("server=(local);database=ElectricalEquipmentInspectionDB;uid=sa;pwd=qweasdzxc;");
con.Open();
}
}
//关闭/销毁函数,相当于Close()
public void Dispose()
{
if (con != null)
{
con.Close();
con = null;
}
}
//以用户名密码登录或者依靠本机号码登录 并获取用户姓名
public string LoginByUser(string user, string pwd)
{
try
{
string sqlStr = "Select 巡检员姓名 From 巡检员信息表 Where 巡检员编号='" + user + "' and 密码='" + pwd + "'";
SqlCommand com = new SqlCommand(sqlStr, con);
SqlDataReader reader = com.ExecuteReader();
string UserName = null;
while (reader.Read())
{
UserName = reader[0].ToString().Trim();
}
reader.Close();
com.Dispose();
return UserName;
}
catch (Exception)
{
return "false";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息