Performing a Bulk Insert with SQL Server


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.


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:


The schema for the data that is bulk loaded into the Customers table

The sample uses a single XML file:


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 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"] +

// . . . 

// Create the bulk load object, defining connection, and error log.
SQLXMLBulkLoad bl = new SQLXMLBulkLoad( );
bl.ConnectionString =

// Execute the bulk load.
 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);
 bl = null;


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

ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton © 2008-2020.
If you may any questions please contact us: