通过上次的《Web服务初探:用Demo学Web服务系列(5)——连接模式访问数据库的Web服务》学习,我们已经知道了,Web Services是如何从数据库中来使用连接模式访问数据库来进行操作。下面我们来看看在上次的讨论中所讲述WebService再次改变,让这个WebService能变成断开模式访问数据库的Web Services。
这次我们要改变上次的WebService时并不是在原来的方法上做改变,而是在WebService中添加了一个新方法,并且在我们建立的C/S程序工程中也加入了一个新的Windows Form来调用这个新加的方法。
一、在前面的WebService中加入下面的方法,代码如下:
二、C/S工程中添加窗体并在“查询”按钮中加入相关代码,窗体和代码如下:
1、窗体中加入一个TextBox、一个Button和一个DataGridView,如下图:
![](https://www.cnblogs.com/images/cnblogs_com/lijigang/WinWebUser.JPG)
2、在其中的“查询”按钮下代码为:
代码说明:跟其他的调用一样我们需要实例化WebService的代理类,然后定义一个DataSet用来接收WebService中SelectUser的返回值,最后将返回的DataSet绑定到DGV_UserView上。
总结:这次我们看见了WebService返回值是DataSet,而在Visual Studio.Net2005中建立的Web Services中可以返回DataTable,这个在Visual Studio.Net2003中是不行的,会提示“不能序列化”。至此我们把C/S程序调用Web Services的简单方法讲述完了,下次随笔中我们讲解一些Web Services的原理知识、Soap消息和XML。
这次我们要改变上次的WebService时并不是在原来的方法上做改变,而是在WebService中添加了一个新方法,并且在我们建立的C/S程序工程中也加入了一个新的Windows Form来调用这个新加的方法。
一、在前面的WebService中加入下面的方法,代码如下:
1
[WebMethod]
2
public DataSet SelectUser(string UserName)
3
{
4
Configuration WebConfig = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
5
DataSet DS = new DataSet("WSDemoDB");
6
DataTable DT = new DataTable("UserTable");
7
DS.Namespace = "http://tempuri.org/DataSet";
8
DS.Tables.Add(DT);
9
if (WebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
10
{
11
ConnectionStringSettings ConStr = WebConfig.ConnectionStrings.ConnectionStrings["WSConStringSQL"];
12
if (ConStr != null)
13
{
14
SqlConnection SqlCon = new SqlConnection(ConStr.ConnectionString);
15
SqlCommand SqlCom = new SqlCommand("SELECT 用户ID, 用户名称, 用户密码, 用户姓名, 用户性别, 密码提示问题, 问题答案 FROM 用户表 WHERE (用户名称 LIKE '%' + @用户名称 + '%')", SqlCon);
16
SqlCom.Parameters.Add("@用户名称", SqlDbType.NVarChar);
17
SqlCom.Parameters["@用户名称"].Value = UserName;
18
SqlDataAdapter SqlDA = new SqlDataAdapter(SqlCom);
19
try
20
{
21
SqlCon.Open();
22
SqlDA.Fill(DT);
23
}
24
finally
25
{
26
SqlDA.Dispose();
27
SqlCom.Dispose();
28
SqlCon.Close();
29
SqlCon.Dispose();
30
}
31
}
32
}
33
return DS;
34
}
WebService方法说明:添加的方法名为SelecUser,其中需要获得一个参数UserName。此方法返回一个根据传入的UserName查询到的相关用户信息的DataSet。![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
二、C/S工程中添加窗体并在“查询”按钮中加入相关代码,窗体和代码如下:
1、窗体中加入一个TextBox、一个Button和一个DataGridView,如下图:
2、在其中的“查询”按钮下代码为:
1
private void Btn_SelectUser_Click(object sender, EventArgs e)
2
{
3
MyServ.MyServiceClass MyWebServ = new MyServ.MyServiceClass();
4
DataSet DS = new DataSet();
5
DS = MyWebServ.SelectUser(TB_User.Text);
6
if (DS.Tables.Count > 0)
7
{
8
DGV_UserView.DataSource = DS.Tables["UserTable"];//此处也可写成:DGV_UserView.DataSource = DS.Tables[0];
9
}
10
else
11
{
12
MessageBox.Show("没有查询到所需要的数据!");
13
}
14
}
这样我们完成了此次WebService的调用。![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
代码说明:跟其他的调用一样我们需要实例化WebService的代理类,然后定义一个DataSet用来接收WebService中SelectUser的返回值,最后将返回的DataSet绑定到DGV_UserView上。
总结:这次我们看见了WebService返回值是DataSet,而在Visual Studio.Net2005中建立的Web Services中可以返回DataTable,这个在Visual Studio.Net2003中是不行的,会提示“不能序列化”。至此我们把C/S程序调用Web Services的简单方法讲述完了,下次随笔中我们讲解一些Web Services的原理知识、Soap消息和XML。
作者对转载者要求说明(以下简称本说明):
1、确保您已经遵守了《中华人民共和国信息网络传播权保护条例》,且必须遵守《刚刚网络作品版权声明》(若两文件有冲突内容以《中华人民共和国信息网络传播权保护条例》为准,但其他非冲突内容依然各自有效),再转载。
2、“本说明、作者、作者博客网址及作者博客坐落,本文中提及的各种说明、备注或附录性文字”必须被转载,且不得改变其原有内容和要表达的意图!
作者:刚刚 作者博客网址:http://lijigang.cnblogs.com/ 作者博客坐落在博客园
把握现实生活,培养自身能力
掌握新型技术,提高自我力量