SQLXML 3.0 Bulkload functionality is provided via a COM object (xblkld3.dll). This component can be used in .NET through COM-interop. You can add a reference to xblkld3.dll COM DLL (Project | Add Reference…| COM tab and select Microsoft SQLXML Bulkload 3.0 Type Library) or use tlbimp.exe to create a managed wrapper for the COM DLL that can be used from within the .NET code. The Add Reference… creates an assembly file named Interop.SQLXMLBULKLOADLib.dll, and now you can use the following code to bulk load the XML file into SQL Server:
using SQLXMLBULKLOADLib;
…
…
SQLXMLBulkLoad3Class bulkLoad = new SQLXMLBulkLoad3Class();
bulkLoad.ConnectionString = txtDBConnStr.Text;
bulkLoad.Execute(txtSchemaFile.Text, txtSourceXML.Text); Click here to download the sample C# application that illustrates this.
Note that SQLXML Bulkload component will not run in a multi-threaded environment, and will produce the InvalidCastException exception "QueryInterface for interface SQLXMLBULKLOADLib.ISQLXMLBulkLoad failed.". To fix this, either use the [STAThread] attribute or write the following line:
System.Threading.Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA;
Related Links:
- SQLXML Bulkload KB Articles
- SQLXML FAQs: Bulkload
- Using XML Bulk Load to Load ADO-Generated XML Data
- XML Bulk Load Overview
- SQLXML 3.0 SP1
|