Danh mục

Updating Server Data Using a Web Service

Số trang: 6      Loại file: pdf      Dung lượng: 18.51 KB      Lượt xem: 11      Lượt tải: 0    
Thu Hiền

Hỗ trợ phí lưu trữ khi tải xuống: 2,000 VND Tải xuống file đầy đủ (6 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

[ Team LiB ] Recipe 4.11 Updating Server Data Using a Web Service Problem You want to update a data source using an XML web service and use the web service from your client application. Solution Use a DataSet object. The XML web service code contains two methods: LoadOrders( )
Nội dung trích xuất từ tài liệu:
Updating Server Data Using a Web Service[ Team LiB ]Recipe 4.11 Updating Server Data Using a Web ServiceProblemYou want to update a data source using an XML web service and use the web servicefrom your client application.SolutionUse a DataSet object.The XML web service code contains two methods:LoadOrders( ) Creates and returns a DataSet containing the Orders and Order Details tables from Northwind and a DataRelation between those tables.UpdateOrders( ) Takes a DataSet argument containing the changes made to the DataSet created by the LoadOrders( ) method, creates two DataAdapter objects with CommandBuilder generated update logic for each, and uses the DataAdapter objects to update the Orders and Order Details tables in Northwind.The client-side code contains two event handlers:Form.Load Sets up the example by calling the LoadOrders( ) method in the web service to populate a DataSet. The default view of the Orders table is bound to the data grid on the form.Update Button.Click Calls the UpdateOrders( ) method in the web service passing a DataSet containing changes made to the DataSet since the form was loaded or since the last time the UpdateOrders( ) method was called.The C# code for the XML web service is shown in Example 4-25.Example 4-25. File: NorthwindServiceCS.asmx.cs// Namespaces, variables, and constantsusing System;using System.ComponentModel;using System.Web.Services;using System.Configuration;using System.Data;using System.Data.SqlClient;public const String ORDERS_TABLE = Orders;public const String ORDERDETAILS_TABLE = OrderDetails;public const String ORDERID_FIELD = OrderID;public const String ORDERS_ORDERDETAILS_RELATION = Order_OrderDetails_Relation;// . . .[WebMethod]public DataSet LoadOrders( ){ DataSet ds = new DataSet( ); SqlDataAdapter da; // Fill the Order table and add it to the DataSet. da = new SqlDataAdapter(SELECT * FROM Orders, ConfigurationSettings.AppSettings[DataConnectString]); DataTable orderTable = new DataTable(ORDERS_TABLE); da.FillSchema(orderTable, SchemaType.Source); da.Fill(orderTable); ds.Tables.Add(orderTable); // Fill the OrderDetails table and add it to the DataSet. da = new SqlDataAdapter(SELECT * FROM [Order Details], ConfigurationSettings.AppSettings[DataConnectString]); DataTable orderDetailTable = new DataTable(ORDERDETAILS_TABLE); da.FillSchema(orderDetailTable, SchemaType.Source); da.Fill(orderDetailTable); ds.Tables.Add(orderDetailTable); // Create a relation between the tables. ds.Relations.Add(ORDERS_ORDERDETAILS_RELATION, ds.Tables[ORDERS_TABLE].Columns[ORDERID_FIELD], ds.Tables[ORDERDETAILS_TABLE].Columns[ORDERID_FIELD], true); return ds;}[WebMethod]public bool UpdateOrders(DataSet ds){ // Create the DataAdapters for order and order details tables. SqlDataAdapter daOrders = new SqlDataAdapter(SELECT * FROM Orders, ConfigurationSettings.AppSettings[DataConnectString]); SqlDataAdapter daOrderDetails = new SqlDataAdapter(SELECT * FROM [Order Details], ConfigurationSettings.AppSettings[DataConnectString]); // Use CommandBuilder to generate update logic. SqlCommandBuilder cbOrders = new SqlCommandBuilder(daOrders); SqlCommandBuilder cbOrderDetails = new SqlCommandBuilder(daOrderDetails); // Update parent and child records. daOrderDetails.Update(ds.Tables[ORDERDETAILS_TABLE].Select(null, null, DataViewRowState.Deleted)); daOrders.Update(ds.Tables[ORDERS_TABLE].Select(null, null, DataViewRowState.Deleted)); daOrders.Update(ds.Tables[ORDERS_TABLE].Select(null, null, DataViewRowState.ModifiedCurrent)); daOrders.Update(ds.Tables[ORDERS_TABLE].Select(null, null, DataViewRowState.Added)); daOrderDetails.Update(ds.Tables[ORDERDETAILS_TABLE].Select(null, null, DataViewRowState.ModifiedCurrent)); daOrderDetails.Update(ds.Tables[ORDERDETAILS_TABLE].Select(null, null, DataViewRowState.Added)); return true;}The C# web services client-side code is shown in Example 4-26.Example 4-26. File: UpdateServerThroughWebServiceForm.cs// Namespaces, variables, and constantsusing System;using System.Windows.Forms;using System.Data;// Table name constantsprivate const String ORDERS_TABLE = Orders;private const String ORDERDETAILS_TABLE = OrderDetails;private DataSet ds;// . . .private void UpdateServerThroughWebServiceForm_Load(object sender, System.EventArgs e){ Cursor.Current = Cursors.WaitCursor; // Create the Web Service object. NorthwindServiceCS nws = new NorthwindServiceCS( ); // Load the DataSet containing orders and order details. ds = nws.LoadOrders( ); // Bind the default view of the orders table to the grid. dataGrid.DataSource = ds.Tables[ORDERS_TABLE].DefaultView; Cursor.Current = Cursors.Default;}private void updateButton_Click(object sender, System.EventArgs e){ Cursor.Current = Cursors.WaitCursor; // Get the changes to the data. DataSet dsChanges = ds.GetChanges( ); if (dsChanges!=null) { // Create the Web Service object. NorthwindServiceCS nws = new NorthwindServiceCS( ); // Update the changes to the order and order detail // informatation. bool retVal = nws.UpdateOrders(dsChanges); } Cursor.Current = Cursors.Default;}DiscussionAn XML web service is software that is accessible using Internet standards such as XMLand HTTP. Because they are accessible through open-standard interfaces, web servicesmake it easy to allow heterogeneous systems to work together..NET makes it very easy to build XML web services. In .NET, web services areimplemented as .ASMX files beginning with a @WebService directive. For example, thesolution code contains ...

Tài liệu được xem nhiều: