Problem
Given many records in an XML file that you need to add to a SQL Server 2000 database, you need to perform a bulk insert with optimal performance.
Solution
Perform a fast bulk insert and update using the XML bulk load functionality in Microsoft SQL Server 2000.
You'll need a reference to the Microsoft SQLXML BulkLoad 3.0 Type Library from the COM tab in Visual Studio .NET's Add Reference Dialog.
The sample uses a single XSD file:
Customers.xsd
The schema for the data that is bulk loaded into the Customers table
The sample uses a single XML file:
Customers.xml
Contains the data that is bulk loaded into the Customers table
The sample code creates a bulk load object SQLXMLBulkLoad and sets the connection string and error log file for the object. The Execute( ) method of the SQLXMLBulkLoad object is used to bulk load the Customers data in the XML file into the Customers table in the Northwind database. The Customers table must be empty prior to running this sample, otherwise , a primary key constraint error will be raised and written to the error log.
The Customers XSD file is shown in Example 9-7, and the XML file is shown in Example 9-8.
Example 9-7. File: Customers.xsd
Example 9-8. File: Customers.xml
ALFKI Alfreds Futterkiste Maria Anders Sales Representative
Obere Str. 57
Berlin 12209 Germany 030-0074321 030-0076545 WOLZA Wolski Zajazd Zbyszek Piestrzeniewicz Owner
ul. Filtrowa 68
Warszawa 01-012 Poland (26) 642-7012 (26) 642-7012
The C# code is shown in Example 9-9.
Example 9-9. File: BulkInsertForm.cs
// Namespaces, variables, and constants using System; using System.Configuration; using System.Windows.Forms; using SQLXMLBULKLOADLib; using System.Data; using System.Data.SqlClient; private const String DATAFILENAME = ConfigurationSettings.AppSettings["Project_Directory"] + @"Chapter 09Customers.xml"; private const String SCHEMAFILENAME = ConfigurationSettings.AppSettings["Project_Directory"] + @"Chapter 09Customers.xsd"; private const String ERRORLOGFILENAME = ConfigurationSettings.AppSettings["Temp_Directory"] + "BulkLoadError.log"; // . . . // Create the bulk load object, defining connection, and error log. SQLXMLBulkLoad bl = new SQLXMLBulkLoad( ); bl.ConnectionString = ConfigurationSettings.AppSettings["OleDb_Msde_ConnectString"]; bl.ErrorLogFile = ERRORLOGFILENAME; // Execute the bulk load. try { bl.Execute(SCHEMAFILENAME, DATAFILENAME); MessageBox.Show("Bulk load completed successfully.", "Bulk Load", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception) { MessageBox.Show("ERROR. See " + ERRORLOGFILENAME + " for details.", "Bulk Load Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { bl = null; }
Discussion
The SQL Server XML Bulk Load component is used through COM interop to bulk insert data contained in a XML document into a SQL Server database. This component controls the execution of a XML bulk load operation. The example defines an optional error log file, where the default is an empty string meaning that no error log is created.
You can bulk load data into multiple parent-child tables at the same time, a feature that is not available in the OpenXML Transact-SQL extension.
For information about the XML Bulk Load component and its methods and properties, see the topic "XML Bulk Load" in the MSDN Library.
Connecting to Data
Retrieving and Managing Data
Searching and Analyzing Data
Adding and Modifying Data
Copying and Transferring Data
Maintaining Database Integrity
Binding Data to .NET User Interfaces
Working with XML
Optimizing .NET Data Access
Enumerating and Maintaining Database Objects
Appendix A. Converting from C# to VB Syntax