Using a Single Stored Procedure to Update Multiple Changes to a SQL Server Database
Số trang: 7
Loại file: pdf
Dung lượng: 23.95 KB
Lượt xem: 12
Lượt tải: 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 8.11 Using a Single Stored Procedure to Update Multiple Changes to a SQL Server Database Problem You need to update a SQL Server 2000 database with changes to multiple rows in a DataSet by executing a single stored procedure.
Nội dung trích xuất từ tài liệu:
Using a Single Stored Procedure to Update Multiple Changes to a SQL Server Database[ Team LiB ]Recipe 8.11 Using a Single Stored Procedure to Update Multiple Changes to a SQLServer DatabaseProblemYou need to update a SQL Server 2000 database with changes to multiple rows in aDataSet by executing a single stored procedure.SolutionUse OpenXML with an XMLdocument representing a DataSet of the changes made.The schema of table TBL0811 used in this solution is shown in Table 8-10. Table 8-10. TBL0811 schema Column name Data type Length Allow nulls?Id int 4 NoField1 nvarchar 50 YesField2 nvarchar 50 YesExample 8-16 uses a single stored procedure:SP0811_Update Used to update the table TBL0811 with the changes made to the DataSet passed in as an NText input parameter @data. The parameters @data and @datadeleted contain an XML representation of a DataSet containing all updated and added records and all deleted records, respectively. These parameters are parsed using the system stored procedure sp_xml_preparedocument that returns a handle that is subsequently used to access the parsed XML document. OpenXML is used to update, insert, and delete the DataSet changes made to TBL0811. Finally, the system stored procedure sp_xml_removedocument is used to free the memory used by the parsed XML documents.The sample code contains two event handlers:Form.Load Sets up the sample by creating a DataSet containing the contents of the table TBL0811. The ColumnMapping for each column is set to MappingType.Attribute. The default view of the table is bound to the data grid on the form.Update Button.Click Writes the XML representation of the added and changed records in the DataSet to the stored procedure NText parameter @data and the XML representation of deleted records in the DataSet to the stored procedure NText parameter @datadelete. The stored procedure SP0811_Update is called to update the database with the batched changes.Example 8-16. Stored procedure: SP0811_UpdateALTER PROC SP0811_Update @data ntext = null, @datadelete ntext = nullASDECLARE @hDoc int-- updated and inserted recordsif @data is not nullbegin EXEC sp_xml_preparedocument @hDoc OUTPUT, @data UPDATE TBL0811 SET TBL0811.Field1 = XmlTBL0811.Field1, TBL0811.Field2 = XmlTBL0811.Field2 FROM OPENXML(@hDoc, NewDataSet/TBL0811) WITH ( Id Integer, Field1 nvarchar(50), Field2 nvarchar(50) ) XmlTBL0811 WHERE TBL0811.Id = XmlTBL0811.Id INSERT INTO TBL0811 SELECT Id, Field1, Field2 FROM OPENXML(@hdoc, NewDataSet/TBL0811) WITH ( Id Integer, Field1 nvarchar(50), Field2 nvarchar(50) ) XmlTBL0811 WHERE XmlTBL0811.Id NOT IN (SELECT Id from TBL0811) EXEC sp_xml_removedocument @hDocend-- deleted recordsif @datadelete is not nullbegin EXEC sp_xml_preparedocument @hDoc OUTPUT, @datadelete DELETE TBL0811 FROM TBL0811 INNER JOIN OPENXML(@hDoc, NewDataSet/TBL0811) WITH ( Id Integer, Field1 nvarchar(50), Field2 nvarchar(50) ) XmlTBL0811 ON TBL0811.Id = XmlTBL0811.Id EXEC sp_xml_removedocument @hDocendThe C# code is shown in Example 8-17.Example 8-17. File: StoredProcedureMultipleRowsForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Windows.Forms;using System.Text;using System.IO;using System.Data;using System.Data.SqlClient;private DataSet ds;private const String TABLENAME = TBL0811;private const String STOREDPROCEDURE_NAME = SP0811_Update;// . . .private void StoredProcedureMultipleRowsForm_Load(object sender, System.EventArgs e){ ds = new DataSet( ); // Create the DataAdapter. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM + TABLENAME, ConfigurationSettings.AppSettings[Sql_ConnectString]); // Load the schema and data for the table. da.FillSchema(ds, SchemaType.Source, TABLENAME); da.Fill(ds, TABLENAME); // Columns in XML representation of data as attributes foreach(DataColumn col in ds.Tables[TABLENAME].Columns) col.ColumnMapping = MappingType.Attribute; // This technique supports only update and insert; turn off delete // records in the default view. ds.Tables[TABLENAME].DefaultView.AllowDelete = false; // Bind the default view of the table to the grid. dataGrid.DataSource = ds.Tables[TABLENAME].DefaultView;}private void updateButton_Click(object sender, System.EventArgs e){ StringBuilder sb; StringWriter sw; // Create a connection and command for the update stored procedur ...
Nội dung trích xuất từ tài liệu:
Using a Single Stored Procedure to Update Multiple Changes to a SQL Server Database[ Team LiB ]Recipe 8.11 Using a Single Stored Procedure to Update Multiple Changes to a SQLServer DatabaseProblemYou need to update a SQL Server 2000 database with changes to multiple rows in aDataSet by executing a single stored procedure.SolutionUse OpenXML with an XMLdocument representing a DataSet of the changes made.The schema of table TBL0811 used in this solution is shown in Table 8-10. Table 8-10. TBL0811 schema Column name Data type Length Allow nulls?Id int 4 NoField1 nvarchar 50 YesField2 nvarchar 50 YesExample 8-16 uses a single stored procedure:SP0811_Update Used to update the table TBL0811 with the changes made to the DataSet passed in as an NText input parameter @data. The parameters @data and @datadeleted contain an XML representation of a DataSet containing all updated and added records and all deleted records, respectively. These parameters are parsed using the system stored procedure sp_xml_preparedocument that returns a handle that is subsequently used to access the parsed XML document. OpenXML is used to update, insert, and delete the DataSet changes made to TBL0811. Finally, the system stored procedure sp_xml_removedocument is used to free the memory used by the parsed XML documents.The sample code contains two event handlers:Form.Load Sets up the sample by creating a DataSet containing the contents of the table TBL0811. The ColumnMapping for each column is set to MappingType.Attribute. The default view of the table is bound to the data grid on the form.Update Button.Click Writes the XML representation of the added and changed records in the DataSet to the stored procedure NText parameter @data and the XML representation of deleted records in the DataSet to the stored procedure NText parameter @datadelete. The stored procedure SP0811_Update is called to update the database with the batched changes.Example 8-16. Stored procedure: SP0811_UpdateALTER PROC SP0811_Update @data ntext = null, @datadelete ntext = nullASDECLARE @hDoc int-- updated and inserted recordsif @data is not nullbegin EXEC sp_xml_preparedocument @hDoc OUTPUT, @data UPDATE TBL0811 SET TBL0811.Field1 = XmlTBL0811.Field1, TBL0811.Field2 = XmlTBL0811.Field2 FROM OPENXML(@hDoc, NewDataSet/TBL0811) WITH ( Id Integer, Field1 nvarchar(50), Field2 nvarchar(50) ) XmlTBL0811 WHERE TBL0811.Id = XmlTBL0811.Id INSERT INTO TBL0811 SELECT Id, Field1, Field2 FROM OPENXML(@hdoc, NewDataSet/TBL0811) WITH ( Id Integer, Field1 nvarchar(50), Field2 nvarchar(50) ) XmlTBL0811 WHERE XmlTBL0811.Id NOT IN (SELECT Id from TBL0811) EXEC sp_xml_removedocument @hDocend-- deleted recordsif @datadelete is not nullbegin EXEC sp_xml_preparedocument @hDoc OUTPUT, @datadelete DELETE TBL0811 FROM TBL0811 INNER JOIN OPENXML(@hDoc, NewDataSet/TBL0811) WITH ( Id Integer, Field1 nvarchar(50), Field2 nvarchar(50) ) XmlTBL0811 ON TBL0811.Id = XmlTBL0811.Id EXEC sp_xml_removedocument @hDocendThe C# code is shown in Example 8-17.Example 8-17. File: StoredProcedureMultipleRowsForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Windows.Forms;using System.Text;using System.IO;using System.Data;using System.Data.SqlClient;private DataSet ds;private const String TABLENAME = TBL0811;private const String STOREDPROCEDURE_NAME = SP0811_Update;// . . .private void StoredProcedureMultipleRowsForm_Load(object sender, System.EventArgs e){ ds = new DataSet( ); // Create the DataAdapter. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM + TABLENAME, ConfigurationSettings.AppSettings[Sql_ConnectString]); // Load the schema and data for the table. da.FillSchema(ds, SchemaType.Source, TABLENAME); da.Fill(ds, TABLENAME); // Columns in XML representation of data as attributes foreach(DataColumn col in ds.Tables[TABLENAME].Columns) col.ColumnMapping = MappingType.Attribute; // This technique supports only update and insert; turn off delete // records in the default view. ds.Tables[TABLENAME].DefaultView.AllowDelete = false; // Bind the default view of the table to the grid. dataGrid.DataSource = ds.Tables[TABLENAME].DefaultView;}private void updateButton_Click(object sender, System.EventArgs e){ StringBuilder sb; StringWriter sw; // Create a connection and command for the update stored procedur ...
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Using a Single Stored Procedure to Update Multiple Changes to a SQL Server DatabaseTài liệu liên quan:
-
52 trang 434 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 321 0 0 -
74 trang 304 0 0
-
96 trang 299 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 293 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 286 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 270 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0