310143 HOW TO: Using ADO.NET and Visual C # .NET to update server data over Web service (from MSDN)

xiaoxiao2021-03-06  56

The release number of this article has been CHS310143

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

308056.

For Microsoft Visual J # .NET versions of this article, see

320634.

This article references the following Microsoft .NET Framework Class Bank Name Space:

System.data.sqlclient

This task content

summary

Create a Web service test web service creation client application test client application reference

Summary This step-by-step guide demonstrates how to use

DataSet objects receive and update database data over a web service. This article also demonstrates how to reference web services in the client application and

Displayed in the DataGrid control

DataSet so that you can update the data and send the update back to the server.

Note: For updates in a single table, you can only use the methods in this article.

Back to top

Require the following list lists the recommended hardware, software, network structure, and service pack required:

Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server Microsoft Visual Studio .NET This article assumes that you are familiar with the following topics:

Visual Studio .NET ADO.NET Basic Knowledge and Syntax ASP.NET Basics and Syntax This example This article uses http: // localhost as a web server. In addition, the code example is used.

Rosewin database.

The Ross Wen Database is included in Microsoft SQL Server.

Back to top

Create a web service

Start Visual Studio .NET. Follow these steps to create a Visual C # ASP.NET Web service item:

On the File menu, point to New, and then click Project. In the New Project dialog box, click the Visual C # item below the Project Type, and then click the ASP.NET Web service below the template. In the location box, type the URL of the server and project name CSUPDATEDATA (for example, http: // localhost / csupdatedata). The http: // localhost section of the URL runs a web service on the local web server. Click OK. On the Service1.asmx.cs [Design] tab, right-click the page, and then click View Code to switch to "Code" view. The "Code" window of the Web service will appear. Add the following USING statement to the top of the code window: // Use Data Access Objects from The Sqlclient Namespace.

Using system.data.sqlclient; public service1 after the following code

{

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

InitializationComponent ();

} Add the following code: [WebMethod]

Public Dataset getCustomers ()

{

SqlConnection Con = New SqlConnection ("Server = ServerName; UID = Login)

PWD = password; database = northwind ");

Sqldataadapter Dacust = New SqldataAdapter ("Select * from customers", con); dataset ds = new dataset ();

Dacust.Fill (DS, "Cust");

Return DS;

}

[WebMethod]

Public DataSet UpdateCustomers (DataSet DS)

{

SqlConnection Con = New SqlConnection ("Server = ServerName; UID = Login)

PWD = password; database = northwind ");

SqlDataAdapter Dacust = New SqldataAdapter ("Select * from customers", con);

Sqlcommandbuilder cbcust = new sqlcommandbuilder (DACUST);

Dacust.Update (DS, "CUST");

Return DS;

} Modify the SqlConnection string to properly connect to a computer that is running SQL Server.

Back to top

Test web service

Press F5 to compile and run the web service. A web page will be displayed, which allows you to interact with the Web service in Microsoft Internet Explorer. Note that the URL of the return page is http: //localhost/csupdatedata/service1.asmx. On the Service1 Web page, click GetCustomers. You will see a web page that includes details about the getCustomers web method. Close the web page.

Back to top

Create a client application

In Visual Studio .NET, create a new Visual C # Windows application project. The FORM1 is added to the project by default. Add two Button controls and a DataGrid control to Form1. Button1, Button2 and DataGrid1 are added to the project by default. Change the TEXT attribute of Button1 to LOAD and change the button of Button2 to Save. In the project menu, click Add Web reference. Type the URL of your web service (for example, type http://localhost/csupdatedata/service1.asmx), press ENTER, and then click Add Reference. The newly added Web reference entry appears in the View menu of the Solution Explorer. In the Visual C # project, open the code window of Button1. Add the following code to the Button1_Click (Load) event: localhost.service1 myservice = new localhost.service1 ();

DataGrid1.datasource = myService.getCustomers ();

DataGrid1.DataMember = "CUST"; switch to the form view. Open the code window of Button2. Add the following code to the Button2_Click (Save) event: localhost.service1 myservice = new localhost.service1 ();

DataSet DS = (Dataset) DataGrid1.datasource;

DataSet DSChanges = ds.getchanges (); if (dschange! = Null)

{

DS.MERGE (MyService.UpdateCustomers (DSChanges), True;

}

Back to top

Test client application

Press F5 to compile and run the client application. Please note that the initial DataGrid1 is empty. Click Load. Note DataGrid1 now displays customer records. In DataGrid1, modify some data, then click Save. Note: Do not change the key field. If you change the key field, you will receive an error message indicating that you ruin the reference integrity on the server.

Back to top

For additional information, see "Creating and Accessing the Web Service Drive" topic in the Visual Studio .NET helps documentation.

Back to top

The information in this article applies to:

Microsoft ADO.NET (provided with .NET Frame) Microsoft Visual C # .NET (2002)

Recent Updated: 2002-6-18 (1.0) Keyword KBDsupport Kbhowto KbhowTomaster Kbsqlclient KbsystemData KB310143

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

New Post(0)