提醒注意:上述代码还可以精简,就是把服务接口方法中的SqlConnection1和ds对象的初始化放在构造函数中进行。但要注意,WebService类不同于普通类,它的几个服务接口方法并不共享Service1对象的实例成员,即一个服务接口[WebMothod]方法对实例成员的值的改变不会影响到到其他的个服务接口。在新的服务接口方法被调用时,它所用到的实例成员的初始值会由WebService对象的构造函数或是这个方法本身来决定;
 将Service1.aspx设为启动页,F5执行这个项目,生成解决方案;
 另建一个Windows应用程序项目,默认命名为WindowsApplication1,同时系统自动生成一个Form1.cs的主文件。
 为本项目添加web引用,通过浏览本地的web服务,可以找到前面写入的http://localhost/TestCommunity/Service1.asmx,此时将localhost改为本机名,以确保在本地网其他用户处运行这个桌面程序时能够来查找这台服务器,而不是他自己的机器。笔者的机器名:aishg,所以路径上写入http://aishg/TestCommunity/Service1.asmx,如果是在广域网上使用web引用,请参考UDDI注册和部署等内容,本处不累言。系统默认web服务名为:aishg,选择确定。回到.NET开发窗口,我们看到资源管理器窗口中新添加的web引用:aishg;
 在Form1.cs[设计]页面上,拖入如下控件:
两个Label控件(label1,label2),Text属性分别改为“CustomerID”和“Company”;
两个TextBox控件(textBox1,textBox2),Text属性为空,这两个TextBox分别对应上述两个Label;
三个Button控件(button1,button2,button3),Text属性分别为“AddLocal”、“AddServer”和“ListServer”;
拖入一个DataGrid控件,默认对象名:DataGrid1。
 打开Form1.cs后台代码文件,添加代码,如下所示:
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
                    private DataSet ds1;
private aishg.Service1 AA;

 private System.ComponentModel.Container components = null;
                   
public Form1()
{
InitializeComponent();
AA=new WindowsApplication1.aishg.Service1 ();
BindData();

}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
                ......//省略此处内容,由系统自动生成;

#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());

} private void BindData()
{
ds1=AA.GetDataSet ();
ds1.Tables ["customers"].DefaultView .Sort ="CustomerID";
this.dataGrid1 .DataSource =ds1.Tables["customers"].DefaultView ;
} private void button1_Click(object sender, System.EventArgs e)
{
DataTable customerDT=ds1.Tables ["customers"];
DataRow DR=customerDT.NewRow ();
DR["CustomerID"]=this.textBox1 .Text ;
DR["CompanyName"]=this.textBox2 .Text ;
customerDT.Rows .Add (DR);
ds1.Tables ["customers"].DefaultView .Sort ="CustomerID";
this.dataGrid1 .DataSource =customerDT.DefaultView ;
} private void button2_Click(object sender, System.EventArgs e)
{
AA.InputDataSets (ds1);
BindData();
} private void button3_Click(object sender, System.EventArgs e)
{
BindData();
}
}
}
&#61550; 编译本系统,可看到DataGrid1被Customers表填充的效果。因为本地内存中存在着这样的一个包含Customers表的DataSet,故此也可以在此桌面程序的其他地方使用;在testBox1和testBox2中写入测试数据,分别点击三个Button按钮测试,您将发现有些数据跑在客户本地,直到用户上传,才通过一个DataSet一次性将所有新加内容传至服务器,利用web服务将改变推进数据库中。
&#61550; 另建一个web应用程序项目,默认工程文件名为“WebApplication1”,系统自动添加一个Web页面,文件名为“WebForm1”。
&#61550; 向“WebForm1”中拖入一个DataGrid控件,默认命名DataGrid1。添加我们的web引用,如上面所述,系统将在资源管理器窗口中添加“aishg”引用。(根据机器名而来)。
&#61550; 后台代码,添加如下内容:
    private void Page_Load(object sender, System.EventArgs e)
{
         BindData();
}
    private void BindData()
{
Service1 aa=new Service1 ();
DataSet ds1=aa.GetDataSet ();
ds1.Tables ["customers"].DefaultView .Sort ="CustomerID";
this.DataGrid1 .DataSource =ds1.Tables ["customers"].DefaultView ;
DataGrid1.DataBind ();

}
&#61550; 我们发现代码非常简单,对数据库的连接只需要使用现有的web服务提供的数据源即可。执行程序,生成解决方案,我们看到页面上显示的Customers表的内容。
&#61550; 注意,web应用程序,因为其逻辑和规则部分肯定是运行在服务器内存中的,故此做批量数据添加,一般情况下意义不大,所以我们在B/S的系统中就没有必要使用InputDataSet()的服务向表中添加数据。但是我们可以使用其他的方式建立web服务接口方法,比如向web服务中传递字符串数组,以批量向数据库提交数据,还是有很实际的意义的。
&#61550; 好了,现在两个系统都运行起来了,赶紧根据.NET的帮助文件,看看如何安装和部署项目吧。最好做两个系统的安装程序,放到局域网内的其他机器上跑跑看看:C/S系统添加数据->导入数据库中—>看看B/S下web页面的变化?ok,搞定!小结:
通过上述实例,我们清晰的看到WebService在B/S和C/S双结构系统上的无缝连接,在轻松实现了数据共享的同时,解决了C/S结构系统的部署和维护(尤其是数据库部分)复杂性的障碍,也弥补了B/S结构系统执行效率低下的先天不足。笔者认为:伴随着web服务的广泛应用,双重结构的系统将变得越来越普遍。浏览器作为浏览工具的职能将被突出。数据的频繁交换和系统维护将会由C/S的部分代替:不要再讲C/S的系统的部署和维护麻烦,面对.NET的WebService的开发,你认为部署C/S系统还会有多么麻烦吗?