When importing a large amount of XML data to SQL Server, we usually use XML Bulkload. I recently encountered the following error when migrating an application written in VB to ASP. Net:
The QueryInterface of the SQLXMLBULKLOADLib. ISQLXMLBulkLoad interface fails.
The program code is as follows:
1 private void Page_Load (object sender, System. EventArgs e)
2 {
3 SQLXMLBULKLOADLib. SQLXMLBulkLoad3Class objXBL = new SQLXMLBULKLOADLib. SQLXMLBulkLoad3Class ();
4 objXBL. ConnectionString = "Provider = sqloledb; server = (local); database = Digi; uid = sa; pwd = ";
5 objXBL. ErrorLogFile = "d: \ error. log ";
6 objXBL. KeepIdentity = false;
7 objXBL. Execute ("d :\\ test. xsd", "d :\\ test. xml ");
8}
At first, I thought it was a syntax problem. After debugging, I found that Windows applications in. Net run normally. Only then can we find that it was a Thread problem. The reason why Bulkload can run normally in Windows applications of VB and. Net is that they run in a single thread by default.
So rewrite the program: 1 private void Page_Load (object sender, System. EventArgs e)
2 {
3 Thread bulkLoad = new Thread (new ThreadStart (BulkIt ));
4 bulkLoad. ApartmentState = ApartmentState. STA;
5 bulkLoad. Start ();
6}
7
8
9 public void BulkIt ()
10 {
11 SQLXMLBULKLOADLib. SQLXMLBulkLoad3Class objXBL = new SQLXMLBULKLOADLib. SQLXMLBulkLoad3Class ();
12 objXBL. ConnectionString = "Provider = sqloledb; server = (local); database = Digi; uid = sa; pwd = ";
13 objXBL. ErrorLogFile = "d: \ error. log ";
14 objXBL. KeepIdentity = false;
15 objXBL. Execute ("d :\\ test. xsd", "d :\\ test. xml ");
16}
Running properly! Of course, don't forget to add:
Using System. Threading;