Danh mục tài liệu

Performing a Bulk Insert with SQL Server

Số trang: 5      Loại file: pdf      Dung lượng: 16.98 KB      Lượt xem: 7      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 9.5 Performing a Bulk Insert with SQL Server 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.
Nội dung trích xuất từ tài liệu:
Performing a Bulk Insert with SQL Server[ Team LiB ]Recipe 9.5 Performing a Bulk Insert with SQL ServerProblemGiven 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.SolutionPerform a fast bulk insert and update using the XML bulk load functionality in MicrosoftSQL Server 2000.Youll need a reference to the Microsoft SQLXML BulkLoad 3.0 Type Library from theCOM tab in Visual Studio .NETs Add Reference Dialog.The sample uses a single XSD file:Customers.xsd The schema for the data that is bulk loaded into the Customers tableThe sample uses a single XML file:Customers.xml Contains the data that is bulk loaded into the Customers tableThe sample code creates a bulk load object SQLXMLBulkLoad and sets the connectionstring and error log file for the object. The Execute( ) method of the SQLXMLBulkLoadobject is used to bulk load the Customers data in the XML file into the Customers table inthe Northwind database. The Customers table must be empty prior to running thissample, otherwise, a primary key constraint error will be raised and written to the errorlog.The Customers XSD file is shown in Example 9-7, and the XML file is shown inExample 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 constantsusing 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 09\Customers.xml;private const String SCHEMAFILENAME = ConfigurationSettings.AppSettings[Project_Directory] + @Chapter 09\Customers.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;}DiscussionThe SQL Server XML Bulk Load component is used through COM interop to bulk insertdata contained in a XML document into a SQL Server database. This component controlsthe execution of a XML bulk load operation. The example defines an optional error logfile, 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 isnot 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.[ Team LiB ]