在 Visual C# .NET 中将 Web 服务用作客户端应用程序的数据源

本文介绍如何构建返回DataSet的Web服务,并在客户端应用程序中使用DataGrid显示数据。通过逐步指南,演示了从创建Web服务到测试整个流程的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概要
本文演示如何一步一步创建和测试将 DataSet 对象返回到客户端的 Web 服务。 本文还演示如何在客户端应用程序中引用 Web 服务并在 DataGrid 控件中显示返回的 DataSet

本文中的代码示例使用 http://localhost 作为 Web 服务器。 代码示例仍然使用罗斯文 (Northwind) 数据库,该数据库作为后端数据库包括在 Microsoft SQL Server 中。

创建 Web 服务

1.打开 Microsoft Visual Studio .NET。
2.文件菜单中,指向新建,然后单击项目
3.新建项目对话框中,单击项目类型下的 Visual C#,然后单击模板下的 ASP.NET Web 服务
4.名称文本框中,键入 csCustomer。 在位置文本框中,键入到您的服务器的 URL。 如果您使用的是本地服务器,则键入 http://localhost。单击确定
5.视图菜单中,单击代码以切换到代码视图。
6.将以下代码添加到“代码”窗口顶部紧挨在其他 using 语句之后:
//Use data access objects from the SqlClient namespace.
using System.Data.SqlClient;
7.找到以下代码:
public Service1()
            {
//CODEGEN: This call is required by the ASP.NET Web Services Designer.
InitializeComponent();
            }
在上述代码之后添加以下代码:
[WebMethod]
            public string CountWords(string Sentence)
            {
                  string[] Words = Sentence.Split(' ');
                  return "Your sentence contains " + Words.Length + " word(s).";
            }
[WebMethod]
            public DataSet GetCustOrders(string IDMask)
            {
                  IDMask = IDMask.Replace("'", "''");
//IDMask is the Customer ID that the client submits.
//Replace single quotation marks with two single quotation marks
//so that all single quotation marks in the CustomerID are parsed correctly.

//Modify this connection string to use your SQL Server and log on information.
SqlConnection con = new SqlConnection("server=<your server>;uid=<your user id>;
                                       pwd=<your Password>;database=northwind");

//Open the Customers table to serve as the parent table.
SqlDataAdapter daCust = new SqlDataAdapter("Select * From Customers 
                                            Where CustomerID Like '%" + IDMask + "%'", con);

//Open the Orders table to serve as the child table.
SqlDataAdapter daOrders = new SqlDataAdapter("Select * From Orders 
                                              Where CustomerID Like '%" + IDMask + "%'", con);

//Create a client-side DataSet to hold the Customers and Orders tables.
DataSet ds=new DataSet();

//Explicitly open the connection to allow explicit closing.
con.Open();

//Fill the DataSet with the Customers table and the Orders table.
daCust.Fill(ds, "Cust");
daOrders.Fill(ds, "Orders");

//Explicitly close the connection - don't wait for garbage collection.
con.Close();

//Relate Customers to Orders.
ds.Relations.Add("CustOrd", ds.Tables["Cust"].Columns["CustomerID"],
      ds.Tables["Orders"].Columns["CustomerID"]);

//The relationship is Orders nested within Customers.
ds.Relations[0].Nested = true;

//Return the DataSet to the client.
return ds;
}
8.根据您的环境相应地修改 SqlConnection 字符串。

测试 Web 服务

1.按 F5 键以编译并运行 Web 服务。将返回一个 Web 页,它允许您在 Microsoft Internet Explorer 中与 Web 服务进行交互。

请注意,返回页的 URL 是 http://localhost/csCustomer/Service1.asmx。
2.在 Service1 Web 页上,单击 GetCustOrders。 将返回一个 Web 页,其中显示关于 GetCustOrders Web 方法的详细信息。

请注意,返回页的 URL 是 http://localhost/csCustomer/Service1.asmx?op=GetCustOrders。
3.在 GetCustOrders 页的文本部分,在 IDMask 参数旁边的文本框中键入 AL
4.单击调用。 将返回一个 Web 页,它以分层可扩展标记语言 (XML) 文档的形式显示 GetCustOrders Web 方法的结果。

请注意,返回页的 URL 是 http://localhost/csCustomer/Service1.asmx/GetCustOrders?IDMask=AL。
5.关闭显示的 Web 页。

创建客户端应用程序

1.在 Visual Studio .NET 中,新建一个 Visual C# Windows 应用程序项目。Form1 在默认情况下被添加到项目中。
2.在 Form1 中添加一个 TextBox 控件、一个 Button 控件和一个 DataGrid 控件。 TextBox1Button1 DataGrid1 在默认情况下会被添加到该项目中。
3.项目菜单中,单击添加 Web 引用。 将出现一个显示 Web 引用来源的对话框。
4.添加 Web 引用对话框中,键入 Web 服务的 URL(例如,http://localhost/csCustomer/Service1.asmx),按 ENTER 键,然后单击添加引用。 请注意,在解决方案资源管理器中出现一个名为 Web 引用的新项。
5.在 Visual C# 项目中,双击 Button1 以打开其“代码”窗口,然后将以下代码粘贴到 Button1_Click 事件处理程序中:
       //Use the Web Service that your Web server provides.
       localhost.Service1 MyService = new localhost.Service1();
       //Invoke the public WebMethod that returns a DataSet.
       //Bind the DataGrid to the returned DataSet.
       dataGrid1.DataSource = MyService.GetCustOrders(textBox1.Text);
dataGrid1.DataMember = "Cust";

测试客户端应用程序

1.按 F5 键以编译并运行客户端应用程序。
2.在文本框中键入 AL
3.单击 Button1。 请注意,DataGrid1 显示 CustomerID 字段中包含“AL”的客户记录。
4. DataGrid1 中,单击 ALFKI 旁边的加号 (+),以显示在 Web 方法中定义的 CustOrd 关系。
5.单击 CustOrd,以便按照 CustomerID ALKFI 显示与客户有关的订单。

参考

有关 Web 服务的更多信息,请参阅 Visual Studio .NET 联机帮助文档中的“创建和访问 Web 服务演练”主题。 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值