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.
Solution
Use 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 |
|
4 |
No |
|
Field1 |
|
50 |
Yes |
|
Field2 |
|
50 |
Yes |
Example 8-16 uses a single stored procedure:
SP0811_UpdateUsed to update the table TBL0811 with the changes made to the
DataSetpassed in as anNTextinput parameter@data. The parameters@dataand@datadeletedcontain an XML representation of aDataSetcontaining all updated and added records and all deleted records, respectively. These parameters are parsed using the system stored proceduresp_xml_preparedocumentthat returns a handle that is subsequently used to access the parsed XML document.OpenXMLis used to update, insert, and delete theDataSetchanges made to TBL0811. Finally, the system stored proceduresp_xml_removedocumentis used to free the memory used by the parsed XML documents.
The sample code contains two event handlers:
Form.LoadSets up the sample by creating a
DataSetcontaining the contents of the table TBL0811. TheColumnMappingfor each column is set toMappingType.Attribute. The default view of the table is bound to the data grid on the form. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access