android webservice连接mysql_AndroidStudio手机通过WebService远程访问SqlServer

using System;

using System.Data;

using System.Con figuration;

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.R egularExp ressions;

using System.Collections;

using System.Collections.Generic;

namespace WebApplication1

{

/// <

summary>

///

一个操作数据库的类,所有对SQLServer的操作都写在这个类中,使用的时候实例化一个然后直接调用就可以

/// <

/summary>

public class DBOperation

: IDisposable

{

public static SqlConnection sqlCon;

//用于连接数据库

//下面IP地址适合手机在一个局域网内, 或者是全球唯一地址

private String ConServerStr = @"Data

Source=10.3.1.37;Initial Catalog=book;User

ID=sa;password=123;Integrated Security=True";

//默认构造函数

public DBOperation()

{

if (sqlCon

== null)

{

sqlCon = new

SqlConnection();

sqlCon.ConnectionString =

ConServerStr;

sqlCon.Open();

}

}

//关闭/销毁函数,相当于Close()

public void Dispose()

{

if (sqlCon

!= null)

{

sqlCon.Close();

sqlCon = null;

}

}

/// < summary>

/// 获取所有用户的信息

/// < /summary>

/// < returns> 所有用户信息<

/returns>

public List< string>

selectAllUser()

{

List<

string>  list = new List< string>

();

try

{

string sql = "select * from

[user]";

SqlCommand cmd = new

SqlCommand(sql, sqlCon);

SqlDataReader reader =

cmd.ExecuteReader();

while (reader.Read())

{

//将结果集信息添加到返回向量中

list.Add(reader[0].ToString());

list.Add(reader[1].ToString());

list.Add(reader[2].ToString());

}

reader.Close();

cmd.Dispose();

}

catch (Ex

ception e)

{

}

return

list;

}

/// < summary>

/// 增加用户

/// < /summary>

/// < param name="name"> 名称<

/param>

/// < param name="age"> 年龄<

/param>

public bool insertOneUser(string name, int

age)

{

try

{

string sql = "insert into

[user] (name,age) values ('" + name + "'," + age + ")";

SqlCommand cmd = new

SqlCommand(sql, sqlCon);

cmd.ExecuteNonQuery();

cmd.Dispose();

return true;

}

catch (Ex

ception)

{

return false;

}

}

/// < summary>

/// 删除用户

/// < /summary>

/// < param name="id"> 用户id号<

/param>

public bool deleteOneUser(string id)

{

try

{

string sql = "delete from

[user] where id=" + id;

SqlCommand cmd = new

SqlCommand(sql, sqlCon);

cmd.ExecuteNonQuery();

cmd.Dispose();

return true;

}

catch (Ex

ception)

{

return false;

}

}

}

}

4  WebService1.asmx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using MyProject;

namespace WebApplication1

{

/// <

summary>

/// WebService1

的摘要说明

/// <

/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 WebService1

: System.Web.Services.WebService

{

DBOperation dbOperation = new

DBOperation();

[WebMethod]

public string HelloWorld()

{

return

"Hello World";

}

[WebMethod(Description = "1 获得所有用户")]

public string[] selectAllUser()

{

return

dbOperation.selectAllUser().ToArray();

}

[WebMethod(Description = "2 增加用户")]

public bool insertOneUser(string name, int

age)

{

return

dbOperation.insertOneUser(name, age);

}

[WebMethod(Description = "3 删除用户")]

public bool deleteOneUser(string id)

{

return

dbOperation.deleteOneUser(id);

}

}

}

四 浏览器测试网站访问

创建网站

1  IIS设置

控制面板--程序和功能--打开或关闭Windows功能--Internet信息服务--

在Web管理工具 和 万维网服务 下,勾选相全部勾选

2 新建网站

右击桌面上的

“计算机”--管理--服务和应用程序--Internet信息服务(IIS)管理

在 Internet

信息服务(IIS)管理器中,展开左侧列表,并右击“网站”--“新建网站”

网站名称随便 myWeb1, 物理路径 D:\MyWebService

点 点击右上部“选择”,从中选择 应用程序池

比如:Classic.Net AppPool

端口号:8070

3 入站规则

一机布置多个网站,用端口号来区分,建立 入站的规则

点击 控制面板 右上角 的 查看方式,从 类别 改为 小图标,

控制面板 -- Windows防火墙 -- 左下侧的 高级设置 -- 左侧的 入站规则

-- 右上侧的 新建规则

TCP 方式,端口号:8070 ,其它都默认

4 浏览器测试

http://localhost:8070/WebService1.asmx/selectAllUser

< ArrayOfString

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns="http://tempuri.org/">

< string> 1< /string>

< string> 李芳< /string>

< string> 18< /string>

< string> 2< /string>

< string> 王苗< /string>

< string> 20< /string>

< string> 3< /string>

< string> 王永< /string>

< string> 23< /string>

< /ArrayOfString>

五 手机客户端编程设计

使用 Android Studio 开发</

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值