308495 HOW TO: In Visual C # .NET, the web service is used as the data source of the client application (from mkba)

xiaoxiao2021-03-06  77

The release number of this article has been CHS308495

For Microsoft Visual Basic .NET versions of this article, see

308054.

This task content

summary

Create a Web Service Test Web Services Creating a Client Application Test Client Application Reference

Summary This article demonstrates how to create and test one step by step

The DataSet object returns to the client's Web service. This article also demonstrates how to reference web services in the client application and

Displayed in the DataGrid control

DataSet.

Code examples in this article use

http: // localhost acts as a web server. The code example still uses the Northwind database, which is included as the backend database in Microsoft SQL Server.

Back to top

Create a web service

Open Microsoft Visual Studio .NET. From the file menu, point to New, and then click Project. In the New Project dialog box, click Visual C # under the project type, then click ASP.NET Web Services under Templates. In the Name Text box, type CSCustomer. In the Location Text box, type to your server's URL. Type http: // localhost if you are using a local server. Click OK. From the View menu, click the code to switch to the code view. Add the following code to the top of the Code window next to other USING statements: // Use Data Access Objects from The Sqlclient Namespace.

Using system.data.sqlclient; find the following code: public service1 ()

{

// Codegen: this call is required by the ASP.NET Web Services Designer.

InitializationComponent ();

} Add the following code after the above code: [WebMethod]

Public String Countwords (String Sentence)

{

String [] Words = SENTENCE.SPLIT ('');

Return "Your Sentence Contains" Words.Length "Word (s)."

}

[WebMethod]

Public DataSet getCustRDERS (String idmask)

{

Idmask = idmask.replace ("'", "' '");

// idmask is the customer id what 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 = ; UID = ;

PWD = ; Database = Northwind ");

// Open the customers table.sqldataadapter Dacust = New SqlDataAdapter ("SELECT * home)

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.

C.Open ();

// Fill The DataSet with the customer. Table and the order.

Dacust.Fill (DS, "Cust");

Daorders.Fill (DS, "ORDERS");

// Explicitly Close The Connection - Don't Wait for Garbage Collection.

C. close ();

// Relate Customers to Orders.

DS.RELATIONS.ADD ("CustORD", DS.Tables ["Cust"]. Columns ["Customerid"],

Ds.Tables ["Orders"]. Column ["Customerid"]);

// The Relationship Is Orders Nested within Customers.

DS.RELATIONS [0] .NESTED = true;

// Return the dataset to the client.

Return DS;

} Modify the SqlConnection string accordingly according to your environment.

Back to top

Test web service

Press the F5 key to compile and run the web service. A web page will be returned, which allows you to interact with the Web service in Microsoft Internet Explorer. Note that the URL of the return page is http: //localhost/cscustomer/service1.asmx. On the Service1 Web page, click GetCustRDERS. A web page will be returned, showing more information about the getCustRDERDERS web method. Please note that the URL of the return page is http: //localhost/cscustomer/service1.asmx? Op = getCustRDERS. On the text section of the getCustRDERS page, type Al in the value text box next to the IDMask parameter. Click Call. A web page will be returned, which displays the result of the getCustRDERS Web method in the form of a layered Scalable Markup Language (XML) document. Note that the URL of the return page is http: //localhost/cscustomer/service1.asmx/getcustorders? Idmask = al. Close the web page displayed. Back to top

Create a client application

In Visual Studio .NET, create a new Visual C # Windows application project. Form1 is added to the project by default. Add a TextBox control, a Button control and a DataGrid control in Form1. TextBox1, Button1 and DataGrid1 are added to the project by default. From the project menu, click Add Web Reference. A dialog box will appear. In the Add Web Reference dialog box, type the URL of the web service (for example, http: //localhost/cscustomer/service1.asmx), press Enter, and then click Add Reference. Please note that a new item named web reference is displayed in the Solution Explorer. In the Visual C # project, double-click Button1 to open its Code window, and then paste the following code into the Button1_Click event handler: // 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";

Back to top

Test client application

Press the F5 key to compile and run the client application. Type Al in the text box. Click Button1. Note that DataGrid1 displays a customer record that contains "Al" in the CustomerID field. In DataGrid1, click the plus sign ( ) next to the Alfki to display the CustORD relationship defined in the web method. Click CustORD to display the order related to the customer according to Customerid Alkfi.

Back to top

Refer to more information about Web services, see the Visual Studio .NET online help documentation The theme of "Creating and Accessing Web Service Drive". Back to top

The information in this article applies to:

Microsoft ADO .NET (included in .NET Framework) Microsoft Visual C # .net (2002)

Reserved: 2002-2-15 (1.0) Keyword kbhowto kbhowTomaster KB308495

转载请注明原文地址:https://www.9cbs.com/read-108899.html

New Post(0)