您的位置:首页 > 编程语言 > C#

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

2006-09-06 21:50 387 查看
概要

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

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

创建 Web 服务

loadTOCNode(2, 'summary');
1.打开 Microsoft Visual Studio .NET。
2.文件菜单中,指向新建,然后单击项目
3.新建项目对话框中,单击项目类型下的 Visual C#,然后单击模板下的 ASP.NET Web 服务
4.名称文本框中,键入 csCustomer。 在位置文本框中,键入到您的服务器的 URL。 如果您使用的是本地服务器,则键入 http://localhost。单击确定
5.视图菜单中,单击代码以切换到代码视图。
6.将以下代码添加到“代码”窗口顶部紧挨在其他 using 语句之后:
[code]//Use data access objects from the SqlClient namespace.
using System.Data.SqlClient;

[/code]
7.找到以下代码:
[code]public Service1()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer.
InitializeComponent();
}

[/code]在上述代码之后添加以下代码:
[code][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;
}

[/code]
8.根据您的环境相应地修改 SqlConnection 字符串。

测试 Web 服务

loadTOCNode(2, 'summary');
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 页。

创建客户端应用程序

loadTOCNode(2, 'summary');
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 事件处理程序中:
[code]       //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";

[/code]

测试客户端应用程序

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

参考

loadTOCNode(1, 'references');
有关 Web 服务的更多信息,请参阅 Visual Studio .NET 联机帮助文档中的“创建和访问 Web 服务演练”主题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐