Condividi tramite


Esempi di caricamento in blocco XML (SQLXML 4.0)

Negli esempi seguenti viene illustrata la funzionalità di caricamento bulk XML in Microsoft SQL Server. Ogni esempio fornisce uno schema XSD e lo schema XDR equivalente.

Script del caricatore bulk (ValidateAndBulkload.vbs)

Lo script seguente, scritto in Microsoft Visual Basic Scripting Edition (VBScript), carica un documento XML nel DOM XML; la convalida in base a uno schema; e, se il documento è valido, esegue un caricamento bulk XML per caricare il codice XML in una tabella di SQL Server. Questo script può essere usato con ognuno dei singoli esempi che la fanno riferimento più avanti in questo argomento.

Annotazioni

Il caricamento bulk XML non genera un avviso o un errore se non viene caricato alcun contenuto dal file di dati. Pertanto, è consigliabile convalidare il file di dati XML prima di eseguire un'operazione di caricamento bulk.

Dim FileValid  
  
set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")  
objBL.ConnectionString = "provider=SQLOLEDB;data source=MyServer;database=tempdb;integrated security=SSPI"  
objBL.ErrorLogFile = "c:\error.log"  
  
'Validate the data file prior to bulkload  
Dim sOutput   
sOutput = ValidateFile("SampleXMLData.xml", "", "SampleSchema.xml")  
WScript.Echo sOutput  
  
If FileValid Then  
   ' Check constraints and initiate transaction (if needed)  
   ' objBL.CheckConstraints = True  
   ' objBL.Transaction=True  
  'Execute XML bulkload using file.  
  objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"  
  set objBL=Nothing  
End If  
  
Function ValidateFile(strXmlFile,strUrn,strXsdFile)  
  
   ' Create a schema cache and add SampleSchema.xml to it.  
   Dim xs, fso, sAppPath  
   Set fso = CreateObject("Scripting.FileSystemObject")   
   Set xs = CreateObject("MSXML2.XMLSchemaCache.6.0")  
   sAppPath = fso.GetFolder(".")   
   xs.Add strUrn, sAppPath & "\" & strXsdFile  
  
   ' Create an XML DOMDocument object.  
   Dim xd   
   Set xd = CreateObject("MSXML2.DOMDocument.6.0")  
  
   ' Assign the schema cache to the DOM document.  
   ' schemas collection.  
   Set xd.schemas = xs  
  
   ' Load XML document as DOM document.  
   xd.async = False  
   xd.Load sAppPath & "\" & strXmlFile  
  
   ' Return validation results in message to the user.  
   If xd.parseError.errorCode <> 0 Then  
        ValidateFile = "Validation failed on " & _  
             strXmlFile & vbCrLf & _  
             "=======" & vbCrLf & _  
             "Reason: " & xd.parseError.reason & _  
             vbCrLf & "Source: " & _  
             xd.parseError.srcText & _  
             vbCrLf & "Line: " & _  
             xd.parseError.Line & vbCrLf  
             FileValid = False  
    Else  
        ValidateFile = "Validation succeeded for " & _  
             strXmlFile & vbCrLf & _  
             "========" & _  
             vbCrLf & "Contents to be bulkloaded" & vbCrLf  
             FileValid = True  
    End If  
End Function  

Un. Caricamento bulk di XML in una tabella

In questo esempio viene stabilita una connessione all'istanza di SQL Server specificata nella proprietà ConnectionString (MyServer). Nell'esempio viene inoltre specificata la proprietà ErrorLogFile. Di conseguenza, l'output degli errori viene salvato nel file specificato ("C:\error.log"), che potrebbe anche decidere di passare a un percorso diverso. Si noti anche che il metodo Execute ha come parametri sia il file dello schema di mapping (SampleSchema.xml) che il file di dati XML (SampleXMLData.xml). Quando viene eseguito il caricamento bulk, la tabella Cust creata nel database tempdb conterrà nuovi record in base al contenuto del file di dati XML.

Per testare un carico bulk di esempio

  1. Creare questa tabella:

    CREATE TABLE Cust(CustomerID  int PRIMARY KEY,  
                      CompanyName varchar(20),  
                      City        varchar(20));  
    GO  
    
  2. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleSchema.xml. A questo file aggiungere lo schema XSD seguente:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
       <xsd:element name="ROOT" sql:is-constant="1" >  
         <xsd:complexType>  
           <xsd:sequence>  
             <xsd:element name="Customers" sql:relation="Cust" maxOccurs="unbounded">  
               <xsd:complexType>  
                 <xsd:sequence>  
                   <xsd:element name="CustomerID"  type="xsd:integer" />  
                   <xsd:element name="CompanyName" type="xsd:string" />  
                   <xsd:element name="City"        type="xsd:string" />  
                 </xsd:sequence>  
               </xsd:complexType>  
             </xsd:element>  
           </xsd:sequence>  
          </xsd:complexType>  
         </xsd:element>  
    </xsd:schema>  
    
  3. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleXMLData.xml. A questo file aggiungere il documento XML seguente:

    <ROOT>  
      <Customers>  
        <CustomerID>1111</CustomerID>  
        <CompanyName>Sean Chai</CompanyName>  
        <City>New York</City>  
      </Customers>  
      <Customers>  
        <CustomerID>1112</CustomerID>  
        <CompanyName>Tom Johnston</CompanyName>  
         <City>Los Angeles</City>  
      </Customers>  
      <Customers>  
        <CustomerID>1113</CustomerID>  
        <CompanyName>Institute of Art</CompanyName>  
        <City>Chicago</City>  
      </Customers>  
    </ROOT>  
    
  4. Creare un file nell'editor di testo o XML preferito e salvarlo come ValidateAndBulkload.vbs. A questo file aggiungere il codice VBScript fornito in precedenza all'inizio di questo argomento. Modificare la stringa di connessione per specificare il nome del server appropriato. Specificare il percorso appropriato per i file specificati come parametri per il metodo Execute.

  5. Eseguire il codice VBScript. Il caricamento bulk XML carica il codice XML nella tabella Cust.

Questo è lo schema XDR equivalente:

<?xml version="1.0" ?>  
<Schema xmlns="urn:schemas-microsoft-com:xml-data"   
        xmlns:dt="urn:schemas-microsoft-com:xml:datatypes"    
        xmlns:sql="urn:schemas-microsoft-com:xml-sql" >   
  
   <ElementType name="CustomerID" dt:type="int" />  
   <ElementType name="CompanyName" dt:type="string" />  
   <ElementType name="City" dt:type="string" />  
  
   <ElementType name="ROOT" sql:is-constant="1">  
      <element type="Customers" />  
   </ElementType>  
  
   <ElementType name="Customers"  sql:relation="Cust" >  
      <element type="CustomerID"  sql:field="CustomerID" />  
      <element type="CompanyName" sql:field="CompanyName" />  
      <element type="City"        sql:field="City" />  
  
   </ElementType>  
</Schema>  

B. Caricamento bulk di dati XML in più tabelle

In questo esempio il documento XML è costituito dagli <elementi Customer> e <Order> .

<ROOT>  
  <Customers>  
    <CustomerID>1111</CustomerID>  
    <CompanyName>Sean Chai</CompanyName>  
    <City>NY</City>  
    <Order OrderID="1" />  
    <Order OrderID="2" />  
  </Customers>  
  <Customers>  
    <CustomerID>1112</CustomerID>  
    <CompanyName>Tom Johnston</CompanyName>  
     <City>LA</City>    
    <Order OrderID="3" />  
  </Customers>  
  <Customers>  
    <CustomerID>1113</CustomerID>  
    <CompanyName>Institute of Art</CompanyName>  
    <Order OrderID="4" />  
  </Customers>  
</ROOT>  

Questo esempio carica in blocco i dati XML in due tabelle, Cust e CustOrder:

Cust(CustomerID, CompanyName, City)  
CustOrder(OrderID, CustomerID)  

Lo schema XSD seguente definisce la visualizzazione XML di queste tabelle. Lo schema specifica la relazione padre-figlio tra gli <elementi Customer> e <Order> .

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
  <xsd:annotation>  
    <xsd:appinfo>  
      <sql:relationship name="CustCustOrder"  
          parent="Cust"  
          parent-key="CustomerID"  
          child="CustOrder"  
          child-key="CustomerID" />  
    </xsd:appinfo>  
  </xsd:annotation>  
  <xsd:element name="ROOT" sql:is-constant="1" >  
    <xsd:complexType>  
      <xsd:sequence>  
        <xsd:element name="Customers" sql:relation="Cust" >  
          <xsd:complexType>  
            <xsd:sequence>  
              <xsd:element name="CustomerID"  type="xsd:integer" />  
              <xsd:element name="CompanyName" type="xsd:string" />  
              <xsd:element name="City"        type="xsd:string" />  
              <xsd:element name="Order"   
                          sql:relation="CustOrder"  
                          sql:relationship="CustCustOrder" >  
                <xsd:complexType>  
                  <xsd:attribute name="OrderID" type="xsd:integer" />  
                </xsd:complexType>  
              </xsd:element>  
             </xsd:sequence>  
          </xsd:complexType>  
        </xsd:element>  
      </xsd:sequence>  
    </xsd:complexType>  
  </xsd:element>  
</xsd:schema>  

Il caricamento bulk XML usa la relazione chiave primaria/chiave esterna specificata in precedenza tra gli <elementi Cust> e <CustOrder> per caricare in blocco i dati in entrambe le tabelle.

Per testare un carico bulk di esempio

  1. Creare due tabelle nel database tempdb :

    USE tempdb;  
    CREATE TABLE Cust(  
           CustomerID  int PRIMARY KEY,  
           CompanyName varchar(20),  
           City        varchar(20));  
    CREATE TABLE CustOrder(        OrderID     int PRIMARY KEY,   
            CustomerID int FOREIGN KEY REFERENCES Cust(CustomerID));  
    
  2. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleSchema.xml. Aggiungere lo schema XSD fornito in questo esempio al file .

  3. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleData.xml. Aggiungere il documento XML fornito in precedenza in questo esempio al file .

  4. Creare un file nell'editor di testo o XML preferito e salvarlo come ValidateAndBulkload.vbs. A questo file aggiungere il codice VBScript fornito in precedenza all'inizio di questo argomento. Modificare la stringa di connessione per specificare il server e il nome del database appropriati. Specificare il percorso appropriato per i file specificati come parametri per il metodo Execute.

  5. Eseguire il codice VBScript precedente. Il caricamento bulk XML carica il documento XML nelle tabelle Cust e CustOrder.

Questo è lo schema XDR equivalente:

<?xml version="1.0" ?>  
<Schema xmlns="urn:schemas-microsoft-com:xml-data"   
        xmlns:dt="urn:schemas-microsoft-com:xml:datatypes"    
        xmlns:sql="urn:schemas-microsoft-com:xml-sql" >   
   <ElementType name="CustomerID" dt:type="int" />  
   <ElementType name="CompanyName" dt:type="string" />  
   <ElementType name="City" dt:type="string" />  
  
   <ElementType name="ROOT" sql:is-constant="1">  
      <element type="Customers" />  
   </ElementType>  
  
   <ElementType name="Customers" sql:relation="Cust" >  
      <element type="CustomerID" sql:field="CustomerID" />  
      <element type="CompanyName" sql:field="CompanyName" />  
      <element type="City" sql:field="City" />  
      <element type="Order" >  
<sql:relationship  
                key-relation="Cust"  
                key="CustomerID"  
                foreign-key="CustomerID"  
                foreign-relation="CustOrder" />  
      </element>  
   </ElementType>  
    <ElementType name="Order" sql:relation="CustOrder" >  
      <AttributeType name="OrderID" />  
      <AttributeType name="CustomerID" />  
      <attribute type="OrderID" />  
      <attribute type="CustomerID" />  
    </ElementType>  
</Schema>  

C. Uso delle relazioni di catena nello schema per il caricamento bulk xml

In questo esempio viene illustrato come la relazione M:N specificata nello schema di mapping viene utilizzata dal caricamento bulk XML per caricare i dati in una tabella che rappresenta una relazione M:N.

Si consideri, ad esempio, questo schema XSD:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
<xsd:annotation>  
  <xsd:appinfo>  
    <sql:relationship name="OrderOD"  
          parent="Ord"  
          parent-key="OrderID"  
          child="OrderDetail"  
          child-key="OrderID" />  
  
    <sql:relationship name="ODProduct"  
          parent="OrderDetail"  
          parent-key="ProductID"  
          child="Product"  
          child-key="ProductID"   
          inverse="true"/>  
  </xsd:appinfo>  
</xsd:annotation>  
  
  <xsd:element name="ROOT" sql:is-constant="1" >  
    <xsd:complexType>  
      <xsd:sequence>  
        <xsd:element name="Order"   
                     sql:relation="Ord"   
                     sql:key-fields="OrderID" >  
          <xsd:complexType>  
            <xsd:sequence>  
             <xsd:element name="Product"  
                          sql:relation="Product"   
                          sql:key-fields="ProductID"  
                          sql:relationship="OrderOD ODProduct">  
               <xsd:complexType>  
                 <xsd:attribute name="ProductID" type="xsd:int" />  
                 <xsd:attribute name="ProductName" type="xsd:string" />  
               </xsd:complexType>  
             </xsd:element>  
           </xsd:sequence>  
           <xsd:attribute name="OrderID"   type="xsd:integer" />   
           <xsd:attribute name="CustomerID"   type="xsd:string" />  
         </xsd:complexType>  
       </xsd:element>  
      </xsd:sequence>  
    </xsd:complexType>  
  </xsd:element>  
</xsd:schema>  

Lo schema specifica un <elemento Order> con un <elemento figlio Product> . L'elemento <Order> esegue il mapping alla tabella Ord e l'elemento Product> esegue il< mapping alla tabella Product nel database. La relazione di catena specificata nell'elemento <Product> identifica una relazione M:N rappresentata dalla tabella OrderDetail. Un ordine può includere molti prodotti e un prodotto può essere incluso in molti ordini.

Quando si carica in blocco un documento XML con questo schema, i record vengono aggiunti alle tabelle Ord, Product e OrderDetail.

Per testare un esempio funzionante

  1. Creare tre tabelle:

    CREATE TABLE Ord (  
             OrderID     int  PRIMARY KEY,  
             CustomerID  varchar(5));  
    GO  
    CREATE TABLE Product (  
             ProductID   int PRIMARY KEY,  
             ProductName varchar(20));  
    GO  
    CREATE TABLE OrderDetail (  
           OrderID     int FOREIGN KEY REFERENCES Ord(OrderID),  
           ProductID   int FOREIGN KEY REFERENCES Product(ProductID),  
                       CONSTRAINT OD_key PRIMARY KEY (OrderID, ProductID));  
    GO  
    
  2. Salvare lo schema fornito in precedenza in questo esempio come SampleSchema.xml.

  3. Salvare i dati XML di esempio seguenti come SampleXMLData.xml:

    <ROOT>    
      <Order OrderID="1" CustomerID="ALFKI">  
        <Product ProductID="1" ProductName="Chai" />  
        <Product ProductID="2" ProductName="Chang" />  
      </Order>  
      <Order OrderID="2" CustomerID="ANATR">  
        <Product ProductID="3" ProductName="Aniseed Syrup" />  
        <Product ProductID="4" ProductName="Gumbo Mix" />  
      </Order>  
    </ROOT>  
    
  4. Creare un file nell'editor di testo o XML preferito e salvarlo come ValidateAndBulkload.vbs. A questo file aggiungere il codice VBScript fornito in precedenza all'inizio di questo argomento. Modificare la stringa di connessione per specificare il server e il nome del database appropriati. Rimuovere il commento dalle righe seguenti dal codice sorgente per questo esempio.

    objBL.CheckConstraints = True  
    objBL.Transaction=True  
    
  5. Eseguire il codice VBScript. Il caricamento bulk XML carica il documento XML nelle tabelle Ord e Product.

D. Caricamento bulk nelle colonne di tipo Identity

In questo esempio viene illustrato come il caricamento bulk gestisce le colonne di tipo Identity. Nell'esempio i dati vengono caricati in blocco in tre tabelle (Ord, Product e OrderDetail).

In queste tabelle:

  • OrderID nella tabella Ord è una colonna di tipo Identity

  • ProductID nella tabella Product è una colonna di tipo Identity.

  • Le colonne OrderID e ProductID in OrderDetail sono colonne chiave esterna che fanno riferimento alle colonne chiave primaria corrispondenti nelle tabelle Ord e Product.

Di seguito sono riportati gli schemi di tabella per questo esempio:

Ord (OrderID, CustomerID)  
Product (ProductID, ProductName)  
OrderDetail (OrderID, ProductID)  

In questo esempio di caricamento bulk XML, la proprietà KeepIdentity del modello a oggetti BulkLoad è impostata su false. Di conseguenza, SQL Server genera valori Identity per le colonne ProductID e OrderID nelle tabelle Product e Ord, rispettivamente (tutti i valori forniti nei documenti da caricare in blocco vengono ignorati).

In questo caso, il caricamento bulk XML identifica la relazione chiave primaria/chiave esterna tra le tabelle. Il caricamento bulk inserisce prima i record nelle tabelle con la chiave primaria, quindi propaga il valore Identity generato da SQL Server alle tabelle con colonne chiave esterna. Nell'esempio seguente il caricamento bulk XML inserisce i dati nelle tabelle in questo ordine:

  1. Prodotto

  2. Ord

  3. OrderDetail

    Annotazioni

    Per propagare i valori Identity generati nelle tabelle Products e Orders, la logica di elaborazione richiede il caricamento bulk XML per tenere traccia di questi valori per l'inserimento successivo nella tabella OrderDetails. A tale scopo, il caricamento bulk XML crea tabelle intermedie, popola i dati in queste tabelle e successivamente li rimuove.

Per testare un esempio funzionante

  1. Creare queste tabelle:

    CREATE TABLE Ord (  
             OrderID     int identity(1,1)  PRIMARY KEY,  
             CustomerID  varchar(5));  
    GO  
    CREATE TABLE Product (  
             ProductID   int identity(1,1) PRIMARY KEY,  
             ProductName varchar(20));  
    GO  
    CREATE TABLE OrderDetail (  
           OrderID     int FOREIGN KEY REFERENCES Ord(OrderID),  
           ProductID   int FOREIGN KEY REFERENCES Product(ProductID),  
                       CONSTRAINT OD_key PRIMARY KEY (OrderID, ProductID));  
    GO  
    
  2. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleSchema.xml. Aggiungere questo schema XSD a questo file.

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
     <xsd:annotation>  
       <xsd:appinfo>  
        <sql:relationship name="OrderOD"  
              parent="Ord"  
              parent-key="OrderID"  
              child="OrderDetail"  
              child-key="OrderID" />  
        <sql:relationship name="ODProduct"  
              parent="OrderDetail"  
              parent-key="ProductID"  
              child="Product"  
              child-key="ProductID"   
              inverse="true"/>  
       </xsd:appinfo>  
     </xsd:annotation>  
    
      <xsd:element name="Order" sql:relation="Ord"   
                                sql:key-fields="OrderID" >  
       <xsd:complexType>  
         <xsd:sequence>  
            <xsd:element name="Product" sql:relation="Product"   
                         sql:key-fields="ProductID"  
                         sql:relationship="OrderOD ODProduct">  
              <xsd:complexType>  
                 <xsd:attribute name="ProductID" type="xsd:int" />  
                 <xsd:attribute name="ProductName" type="xsd:string" />  
              </xsd:complexType>  
            </xsd:element>  
         </xsd:sequence>  
            <xsd:attribute name="OrderID"   type="xsd:integer" />   
            <xsd:attribute name="CustomerID"   type="xsd:string" />  
        </xsd:complexType>  
      </xsd:element>  
    </xsd:schema>  
    
  3. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleXMLData.xml. Aggiungere il documento XML seguente.

    <ROOT>    
      <Order OrderID="11" CustomerID="ALFKI">  
        <Product ProductID="11" ProductName="Chai" />  
        <Product ProductID="22" ProductName="Chang" />  
      </Order>  
      <Order OrderID="22" CustomerID="ANATR">  
         <Product ProductID="33" ProductName="Aniseed Syrup" />  
        <Product ProductID="44" ProductName="Gumbo Mix" />  
      </Order>  
    </ROOT>  
    
  4. Creare un file nell'editor di testo o XML preferito e salvarlo come ValidateAndBulkload.vbs. A questo file aggiungere il codice VBScript seguente. Modificare la stringa di connessione per specificare il server e il nome del database appropriati. Specificare il percorso appropriato per i file che fungono da parametri per il Execute metodo .

    Set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")  
    objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=tempdb;integrated security=SSPI"  
    objBL.ErrorLogFile = "C:\error.log"  
    objBL.CheckConstraints = True  
    objBL.Transaction = False  
    objBL.KeepIdentity = False  
    objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"  
    Set objBL = Nothing  
    MsgBox "Done."  
    
  5. Eseguire il codice VBScript. Il caricamento bulk XML caricherà i dati nelle tabelle appropriate.

E. Generazione di schemi di tabella prima del caricamento bulk

Il caricamento bulk XML può facoltativamente generare le tabelle se non esistono prima del caricamento bulk. L'impostazione della proprietà SchemaGen dell'oggetto SQLXMLBulkLoad su TRUE esegue questa operazione. È anche possibile richiedere il caricamento bulk XML per eliminare qualsiasi tabella esistente e ricrearle impostando la proprietà SGDropTables su TRUE. Nell'esempio di VBScript seguente viene illustrato l'uso di queste proprietà.

In questo esempio vengono inoltre impostate due proprietà aggiuntive su TRUE:

  • CheckConstraints. L'impostazione di questa proprietà su TRUE garantisce che i dati inseriti nelle tabelle non violano i vincoli specificati nelle tabelle ( in questo caso i vincoli PRIMARY KEY/FOREIGN KEY specificati tra le tabelle Cust e CustOrder). Se si verifica una violazione del vincolo, il caricamento bulk ha esito negativo.

  • XMLFragment. Questa proprietà deve essere impostata su TRUE perché il documento XML di esempio (origine dati) non contiene un singolo elemento di primo livello (e pertanto è un frammento).

Questo è il codice VBScript:

Dim objBL   
Set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")  
objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=tempdb;integrated security=SSPI"  
objBL.ErrorLogFile = "c:\error.log"  
  
objBL.CheckConstraints=true  
objBL.XMLFragment = True  
objBL.SchemaGen = True  
objBL.SGDropTables = True  
  
objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"  
Set objBL = Nothing  

Per testare un esempio funzionante

  1. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleSchema.xml. Aggiungere lo schema XSD fornito nell'esempio precedente, "Using chain relationships in the schema to bulk load XML" al file .

  2. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleXMLData.xml. Aggiungere il documento XML fornito nell'esempio precedente, "Using chain relationships in the schema to bulk load XML" al file . Rimuovere l'elemento <ROOT> dal documento (per renderlo un frammento).

  3. Creare un file nell'editor di testo o XML preferito e salvarlo come ValidateAndBulkload.vbs. A questo file aggiungere il codice VBScript in questo esempio. Modificare la stringa di connessione per specificare il server e il nome del database appropriati. Specificare il percorso appropriato per i file specificati come parametri per il metodo Execute.

  4. Eseguire il codice VBScript. Il caricamento bulk XML crea le tabelle necessarie in base allo schema di mapping fornito e carica in blocco i dati in esso contenuti.

F. Caricamento bulk da un flusso

Il metodo Execute del modello a oggetti caricamento bulk XML accetta due parametri. Il primo parametro è il file dello schema di mapping. Il secondo parametro fornisce i dati XML da caricare nel database. Esistono due modi per passare i dati XML al metodo Execute del caricamento bulk XML:

  • Specificare il nome del file come parametro.

  • Passare un flusso contenente i dati XML.

Questo esempio illustra come eseguire il caricamento bulk da un flusso.

VBScript esegue prima un'istruzione SELECT per recuperare le informazioni sui clienti dalla tabella Customers nel database Northwind. Poiché la clausola FOR XML viene specificata (con l'opzione ELEMENTS) nell'istruzione SELECT, la query restituisce un documento XML incentrato sugli elementi di questo modulo:

<Customer>  
  <CustomerID>..</CustomerID>  
  <CompanyName>..</CompanyName>  
  <City>..</City>  
</Customer>  
...  

Lo script passa quindi il codice XML come flusso al metodo Execute come secondo parametro. Il metodo Execute carica in blocco i dati nella tabella Cust.

Poiché questo script imposta la proprietà SchemaGen su TRUE e SGDropTables su TRUE, il caricamento bulk XML crea la tabella Cust nel database specificato. Se la tabella esiste già, prima elimina la tabella e quindi la ricrea.

Questo è l'esempio VBScript:

Set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")  
Set objCmd = CreateObject("ADODB.Command")  
Set objConn = CreateObject("ADODB.Connection")  
Set objStrmOut = CreateObject ("ADODB.Stream")  
  
objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=tempdb;integrated security=SSPI"  
objBL.ErrorLogFile     = "c:\error.log"  
objBL.CheckConstraints = True  
objBL.SchemaGen        = True  
objBL.SGDropTables     = True  
objBL.XMLFragment      = True  
' Open a connection to the instance of SQL Server to get the source data.  
  
objConn.Open "provider=SQLOLEDB;server=(local);database=tempdb;integrated security=SSPI"  
Set objCmd.ActiveConnection = objConn  
objCmd.CommandText = "SELECT CustomerID, CompanyName, City FROM Customers FOR XML AUTO, ELEMENTS"  
  
' Open the return stream and execute the command.  
Const adCRLF = -1  
Const adExecuteStream = 1024  
objStrmOut.Open  
objStrmOut.LineSeparator = adCRLF  
objCmd.Properties("Output Stream").Value = objStrmOut  
objCmd.Execute , , adExecuteStream  
objStrmOut.Position = 0  
  
' Execute bulk load. Read source XML data from the stream.  
objBL.Execute "SampleSchema.xml", objStrmOut  
  
Set objBL = Nothing  

Lo schema di mapping XSD seguente fornisce le informazioni necessarie per creare la tabella:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
<xsd:element name="ROOT" sql:is-constant="true" >  
  <xsd:complexType>  
    <xsd:sequence>  
      <xsd:element ref="Customers"/>  
    </xsd:sequence>  
  </xsd:complexType>  
</xsd:element>  
<xsd:element name="Customers" sql:relation="Cust" >  
  <xsd:complexType>  
    <xsd:sequence>  
      <xsd:element name="CustomerID"  
                   type="xsd:string"  
                   sql:datatype="nvarchar(5)"/>  
      <xsd:element name="CompanyName"  
                   type="xsd:string"  
                   sql:datatype="nvarchar(40)"/>  
      <xsd:element name="City"  
                   type="xsd:string"  
                   sql:datatype="nvarchar(40)"/>  
    </xsd:sequence>  
  </xsd:complexType>  
</xsd:element>  
</xsd:schema>  

Questo è lo schema XDR equivalente:

<?xml version="1.0" ?>  
<Schema xmlns="urn:schemas-microsoft-com:xml-data"   
        xmlns:dt="urn:schemas-microsoft-com:xml:datatypes"    
        xmlns:sql="urn:schemas-microsoft-com:xml-sql" >  
   <ElementType name="CustomerID" dt:type="int" />  
   <ElementType name="CompanyName" dt:type="string" />  
   <ElementType name="City" dt:type="string" />  
  
   <ElementType name="root" sql:is-constant="1">  
      <element type="Customers" />  
   </ElementType>  
  
   <ElementType name="Customers" sql:relation="Cust"  >  
      <element type="CustomerID" sql:field="CustomerID" />  
      <element type="CompanyName" sql:field="CompanyName" />  
      <element type="City" sql:field="City" />  
    </ElementType>  
</Schema>  

Apertura di un flusso in un file esistente

È anche possibile aprire un flusso in un file di dati XML esistente e passare il flusso come parametro al metodo Execute (anziché passare il nome del file come parametro).

Questo è un esempio di Visual Basic per passare un flusso come parametro:

Private Sub Form_Load()  
Dim objBL As New SQLXMLBulkLoad  
Dim objStrm As New ADODB.Stream  
Dim objFileSystem As New Scripting.FileSystemObject  
Dim objFile As Scripting.TextStream  
  
MsgBox "Begin BulkLoad..."  
objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=tempdb;integrated security=SSPI"  
objBL.ErrorLogFile = "c:\error.log"  
objBL.CheckConstraints = True  
objBL.SchemaGen = True  
objBL.SGDropTables = True  
' Here again a stream is specified that contains the source data   
' (instead of the file name). But this is just an illustration.  
' Usually this is useful if you have an XML data   
' stream that is created by some other means that you want to bulk   
' load. This example starts with an XML text file, so it may not be the   
' best to use a stream (you can specify the file name directly).  
' Here you could have specified the file name itself.   
Set objFile = objFileSystem.OpenTextFile("c:\SampleData.xml")  
objStrm.Open  
objStrm.WriteText objFile.ReadAll  
objStrm.Position = 0  
objBL.Execute "c:\SampleSchema.xml", objStrm  
  
Set objBL = Nothing  
MsgBox "Done."  
End Sub  

Per testare l'applicazione, usare il documento XML seguente in un file (SampleData.xml) e lo schema XSD fornito in questo esempio:

Si tratta dei dati di origine XML (SampleData.xml):

<ROOT>  
  <Customers>  
    <CustomerID>1111</CustomerID>  
    <CompanyName>Hanari Carnes</CompanyName>  
    <City>NY</City>  
    <Order OrderID="1" />  
    <Order OrderID="2" />  
  </Customers>  
  
  <Customers>  
    <CustomerID>1112</CustomerID>  
    <CompanyName>Toms Spezialitten</CompanyName>  
     <City>LA</City>  
    <Order OrderID="3" />  
  </Customers>  
  <Customers>  
    <CustomerID>1113</CustomerID>  
    <CompanyName>Victuailles en stock</CompanyName>  
    <Order CustomerID= "4444" OrderID="4" />  
</Customers>  
</ROOT>  

Questo è lo schema XDR equivalente:

<?xml version="1.0" ?>  
<Schema xmlns="urn:schemas-microsoft-com:xml-data"   
        xmlns:dt="urn:schemas-microsoft-com:xml:datatypes"    
        xmlns:sql="urn:schemas-microsoft-com:xml-sql" >  
  
    <ElementType name="Order" sql:relation="CustOrder" >  
      <AttributeType name="OrderID" />  
      <AttributeType name="CustomerID" />  
      <attribute type="OrderID" />  
      <attribute type="CustomerID" />  
    </ElementType>  
  
   <ElementType name="CustomerID" dt:type="int" />  
   <ElementType name="CompanyName" dt:type="string" />  
   <ElementType name="City" dt:type="string" />  
  
   <ElementType name="root" sql:is-constant="1">  
      <element type="Customers" />  
   </ElementType>  
  
   <ElementType name="Customers" sql:relation="Cust"  >  
      <element type="CustomerID" sql:field="CustomerID" />  
      <element type="CompanyName" sql:field="CompanyName" />  
      <element type="City" sql:field="City" />  
      <element type="Order" >  
             <sql:relationship  
                key-relation="Cust"  
                key="CustomerID"  
                foreign-key="CustomerID"  
                foreign-relation="CustOrder" />  
      </element>  
   </ElementType>  
</Schema>  

G. Caricamento bulk nelle colonne di overflow

Se lo schema di mapping specifica una colonna di overflow utilizzando l'annotazione sql:overflow-field , il caricamento bulk XML copia tutti i dati non utilizzati dal documento di origine in questa colonna.

Si consideri questo schema XSD:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
<xsd:annotation>  
  <xsd:appinfo>  
    <sql:relationship name="CustCustOrder"  
          parent="Cust"  
          parent-key="CustomerID"  
          child="CustOrder"  
          child-key="CustomerID" />  
  </xsd:appinfo>  
</xsd:annotation>  
  <xsd:element name="Customers" sql:relation="Cust"  
                                sql:overflow-field="OverflowColumn" >  
   <xsd:complexType>  
     <xsd:sequence>  
       <xsd:element name="CustomerID"  type="xsd:integer" />  
       <xsd:element name="CompanyName" type="xsd:string" />  
       <xsd:element name="City"        type="xsd:string" />  
       <xsd:element name="Order"   
                          sql:relation="CustOrder"  
                          sql:relationship="CustCustOrder" >  
         <xsd:complexType>  
          <xsd:attribute name="OrderID" type="xsd:integer" />  
          <xsd:attribute name="CustomerID" type="xsd:integer" />  
         </xsd:complexType>  
       </xsd:element>  
     </xsd:sequence>  
    </xsd:complexType>  
  </xsd:element>  
</xsd:schema>  

Lo schema identifica una colonna overflow (OverflowColumn) per la tabella Cust. Di conseguenza, tutti i dati XML non utilizzati per ogni <elemento Customer> vengono aggiunti a questa colonna.

Annotazioni

Tutti gli elementi astratti (elementi per i quali viene specificato abstract="true") e tutti gli attributi non consentiti (attributi per i quali è specificato prohibited="true") vengono considerati overflow dal caricamento bulk XML e vengono aggiunti alla colonna di overflow, se specificato. In caso contrario, vengono ignorati.

Per testare un esempio funzionante

  1. Creare due tabelle nel database tempdb :

    USE tempdb;  
    CREATE TABLE Cust (  
                  CustomerID     int         PRIMARY KEY,  
                  CompanyName    varchar(20) NOT NULL,  
                  City           varchar(20) DEFAULT 'Seattle',  
                  OverflowColumn nvarchar(200));  
    GO  
    CREATE TABLE CustOrder (  
                  OrderID    int PRIMARY KEY,  
                  CustomerID int FOREIGN KEY   
                                 REFERENCES Cust(CustomerID));  
    GO  
    
  2. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleSchema.xml. Aggiungere lo schema XSD fornito in questo esempio al file .

  3. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleXMLData.xml. Aggiungere il documento XML seguente al file:

    <ROOT>  
      <Customers>  
        <CustomerID>1111</CustomerID>  
        <CompanyName>Hanari Carnes</CompanyName>  
        <City><![CDATA[NY]]> </City>  
        <Junk>garbage in overflow</Junk>  
        <Order OrderID="1" />  
        <Order OrderID="2" />  
      </Customers>  
    
      <Customers>  
        <CustomerID>1112</CustomerID>  
        <CompanyName>Toms Spezialitten</CompanyName>  
         <![CDATA[LA]]>   
        <!-- <xyz><address>111 Maple, Seattle</address></xyz>   -->  
        <Order OrderID="3" />  
      </Customers>  
      <Customers>  
        <CustomerID>1113</CustomerID>  
        <CompanyName>Victuailles en stock</CompanyName>  
        <Order OrderID="4" />  
    </Customers>  
    </ROOT>  
    
  4. Creare un file nell'editor di testo o XML preferito e salvarlo come ValidateAndBulkload.vbs. A questo file aggiungere il codice di Microsoft Visual Basic Scripting Edition (VBScript) seguente. Modificare la stringa di connessione per specificare il server e il nome del database appropriati. Specificare il percorso appropriato per i file specificati come parametri per il metodo Execute.

    set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")  
    objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=tempdb;integrated security=SSPI"  
    objBL.ErrorLogFile = "c:\error.log"  
    objBL.CheckConstraints = True  
    objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"  
    set objBL=Nothing  
    
  5. Eseguire il codice VBScript.

Questo è lo schema XDR equivalente:

<?xml version="1.0" ?>  
<Schema xmlns="urn:schemas-microsoft-com:xml-data"   
        xmlns:dt="urn:schemas-microsoft-com:xml:datatypes"    
        xmlns:sql="urn:schemas-microsoft-com:xml-sql" >  
  
    <ElementType name="Order" sql:relation="CustOrder" >  
      <AttributeType name="OrderID" />  
      <AttributeType name="CustomerID" />  
      <attribute type="OrderID" />  
      <attribute type="CustomerID" />  
    </ElementType>  
  
   <ElementType name="CustomerID" dt:type="int" />  
   <ElementType name="CompanyName" dt:type="string" />  
   <ElementType name="City" dt:type="string" />  
  
   <ElementType name="root" sql:is-constant="1">  
      <element type="Customers" />  
   </ElementType>  
  
   <ElementType name="Customers" sql:relation="Cust"   
                       sql:overflow-field="OverflowColumn"  >  
      <element type="CustomerID" sql:field="CustomerID" />  
      <element type="CompanyName" sql:field="CompanyName" />  
      <element type="City" sql:field="City" />  
      <element type="Order" >  
             <sql:relationship  
                key-relation="Cust"  
                key="CustomerID"  
                foreign-key="CustomerID"  
                foreign-relation="CustOrder" />  
      </element>  
   </ElementType>  
</Schema>  

H. Specifica del percorso del file temporaneo in modalità transazione

Quando si esegue il caricamento bulk in modalità transazione, ovvero quando la proprietà Transaction è impostata su TRUE, è necessario impostare anche la proprietà TempFilePath quando viene soddisfatta una delle condizioni seguenti:

  • Si esegue il caricamento bulk in un server remoto.

  • Si vuole usare un'unità o una cartella locale alternativa ,una diversa dal percorso specificato dalla variabile di ambiente TEMP, per archiviare i file temporanei creati nella modalità transazione.

Ad esempio, il codice VBScript seguente carica i dati dal file SampleXMLData.xml nelle tabelle di database in modalità transazione. La proprietà TempFilePath viene specificata per impostare il percorso per i file temporanei generati in modalità transazione.

set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")  
objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=tempdb;integrated security=SSPI"  
objBL.ErrorLogFile = "c:\error.log"  
objBL.CheckConstraints = True  
objBL.Transaction=True  
objBL.TempFilePath="\\Server\MyDir"  
objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"  
set objBL=Nothing  

Annotazioni

Il percorso del file temporaneo deve essere un percorso condiviso accessibile all'account del servizio dell'istanza di destinazione di SQL Server e all'account che esegue l'applicazione di caricamento bulk. A meno che non si stia eseguendo il caricamento bulk in un server locale, il percorso del file temporaneo deve essere un percorso UNC ,ad esempio \\nomeserver\nomecondividente.

Per testare un esempio funzionante

  1. Creare questa tabella nel database tempdb :

    USE tempdb;  
    CREATE TABLE Cust (     CustomerID uniqueidentifier,   
          LastName  varchar(20));  
    GO  
    
  2. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleSchema.xml. Aggiungere lo schema XSD seguente al file:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
      <xsd:element name="ROOT" sql:is-constant="true" >  
        <xsd:complexType>  
          <xsd:sequence>  
            <xsd:element ref="Customers" />  
          </xsd:sequence>  
        </xsd:complexType>  
      </xsd:element>  
    
      <xsd:element name="Customers" sql:relation="Cust" >  
       <xsd:complexType>  
         <xsd:attribute name="CustomerID"  type="xsd:string" />  
         <xsd:attribute name="LastName" type="xsd:string" />  
       </xsd:complexType>  
      </xsd:element>  
    </xsd:schema>  
    
  3. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleXMLData.xml. Aggiungere il documento XML seguente al file:

    <ROOT>  
    <Customers CustomerID="6F9619FF-8B86-D011-B42D-00C04FC964FF"   
               LastName="Smith" />  
    </ROOT>  
    
  4. Creare un file nell'editor di testo o XML preferito e salvarlo come ValidateAndBulkload.vbs. A questo file aggiungere il codice VBScript seguente. Modificare la stringa di connessione per specificare il server e il nome del database appropriati. Specificare il percorso appropriato per i file specificati come parametri per il metodo Execute. Specificare anche il percorso appropriato per la proprietà TempFilePath.

    set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")  
    objBL.ConnectionString = "provider=SQLOLEDB;data source=localhost;database=tempdb;integrated security=SSPI"  
    objBL.ErrorLogFile = "c:\error.log"  
    objBL.CheckConstraints = True  
    objBL.Transaction=True  
    objBL.TempFilePath="\\server\folder"  
    objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"  
    set objBL=Nothing  
    
  5. Eseguire il codice VBScript.

    Lo schema deve specificare il corrispondente sql:datatype per l'attributo CustomerID quando il valore di CustomerID viene specificato come GUID che include parentesi graffe ({ e }), ad esempio:

    <ROOT>  
    <Customers CustomerID="{6F9619FF-8B86-D011-B42D-00C04FC964FF}"   
               LastName="Smith" />  
    </ROOT>  
    

    Questo è lo schema aggiornato:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
      <xsd:element name="ROOT" sql:is-constant="true" >  
        <xsd:complexType>  
          <xsd:sequence>  
            <xsd:element ref="Customers" />  
          </xsd:sequence>  
        </xsd:complexType>  
      </xsd:element>  
    
      <xsd:element name="Customers" sql:relation="Cust" >  
       <xsd:complexType>  
         <xsd:attribute name="CustomerID"  type="xsd:string"   
                        sql:datatype="uniqueidentifier" />  
         <xsd:attribute name="LastName" type="xsd:string" />  
       </xsd:complexType>  
      </xsd:element>  
    </xsd:schema>  
    

    Quando sql:datatype viene specificato l'identificazione del tipo di colonna come uniqueidentifier, l'operazione di caricamento bulk rimuove le parentesi graffe ({ e }) dal valore CustomerID prima di inserirlo nella colonna.

Questo è lo schema XDR equivalente:

<?xml version="1.0" ?>  
<Schema xmlns="urn:schemas-microsoft-com:xml-data"   
        xmlns:dt="urn:schemas-microsoft-com:datatypes"    
        xmlns:sql="urn:schemas-microsoft-com:xml-sql" >  
<ElementType name="ROOT" sql:is-constant="1">  
      <element type="Customers" />  
</ElementType>  
<ElementType name="Customers" sql:relation="Cust" >  
  <AttributeType name="CustomerID"  sql:datatype="uniqueidentifier" />  
  <AttributeType name="LastName"   />  
  
  <attribute type="CustomerID" />  
  <attribute type="LastName"   />  
</ElementType>  
</Schema>  

Io. Utilizzo di una connessione di database esistente con la proprietà ConnectionCommand

È possibile usare una connessione ADO esistente per il caricamento bulk xml. Ciò è utile se il caricamento bulk XML è solo una delle numerose operazioni che verranno eseguite su un'origine dati.

La proprietà ConnectionCommand consente di utilizzare una connessione ADO esistente usando un oggetto comando ADO. Questo è illustrato nell'esempio di Visual Basic seguente:

Private Sub Form_Load()  
Dim objBL As New SQLXMLBulkLoad4  
Dim objCmd As New ADODB.Command  
Dim objConn As New ADODB.Connection  
  
'Open a connection to an instance of SQL Server.  
objConn.Open "provider=SQLOLEDB;data source=(local);database=tempdb;integrated security=SSPI"  
'Ask the Command object to use the connection just established.  
Set objCmd.ActiveConnection = objConn  
  
'Tell Bulk Load to use the active command object that is using the Connection obj.  
objBL.ConnectionCommand = objCmd  
objBL.ErrorLogFile = "c:\error.log"  
objBL.CheckConstraints = True  
'The Transaction property must be set to True if you use ConnectionCommand.  
objBL.Transaction = True  
objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"  
Set objBL = Nothing  
End Sub  

Per testare un esempio funzionante

  1. Creare due tabelle nel database tempdb :

    USE tempdb;  
    CREATE TABLE Cust(  
                   CustomerID   varchar(5) PRIMARY KEY,  
                   CompanyName  varchar(30),  
                   City         varchar(20));  
    GO  
    CREATE TABLE CustOrder(  
                   CustomerID  varchar(5) references Cust (CustomerID),  
                   OrderID     varchar(5) PRIMARY KEY);  
    GO  
    
  2. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleSchema.xml. Aggiungere lo schema XSD seguente al file:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">  
    <xsd:annotation>  
      <xsd:appinfo>  
        <sql:relationship name="CustCustOrder"  
              parent="Cust"  
              parent-key="CustomerID"  
              child="CustOrder"  
              child-key="CustomerID" />  
      </xsd:appinfo>  
    </xsd:annotation>  
      <xsd:element name="ROOT" sql:is-constant="true" >  
        <xsd:complexType>  
          <xsd:sequence>  
            <xsd:element ref="Customers" />  
          </xsd:sequence>  
        </xsd:complexType>  
      </xsd:element>  
      <xsd:element name="Customers" sql:relation="Cust" >  
       <xsd:complexType>  
         <xsd:sequence>  
           <xsd:element name="CustomerID"  type="xsd:integer" />  
           <xsd:element name="CompanyName" type="xsd:string" />  
           <xsd:element name="City"        type="xsd:string" />  
           <xsd:element name="Order"   
                              sql:relation="CustOrder"  
                              sql:relationship="CustCustOrder" >  
             <xsd:complexType>  
              <xsd:attribute name="OrderID" type="xsd:integer" />  
              <xsd:attribute name="CustomerID" type="xsd:integer" />  
             </xsd:complexType>  
           </xsd:element>  
         </xsd:sequence>  
        </xsd:complexType>  
      </xsd:element>  
    </xsd:schema>  
    
  3. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleXMLData.xml. Aggiungere il documento XML seguente al file:

    <ROOT>  
      <Customers>  
        <CustomerID>1111</CustomerID>  
        <CompanyName>Hanari Carnes</CompanyName>  
        <City>NY</City>  
        <Order OrderID="1" />  
        <Order OrderID="2" />  
      </Customers>  
    
      <Customers>  
        <CustomerID>1112</CustomerID>  
        <CompanyName>Toms Spezialitten</CompanyName>  
         <City>LA</City>  
        <Order OrderID="3" />  
      </Customers>  
      <Customers>  
        <CustomerID>1113</CustomerID>  
        <CompanyName>Victuailles en stock</CompanyName>  
        <Order OrderID="4" />  
    </Customers>  
    </ROOT>  
    
  4. Creare un'applicazione Visual Basic (STANDARD EXE) e il codice precedente. Aggiungere questi riferimenti al progetto:

    Microsoft XML BulkLoad for SQL Server 4.0 Type Library  
    Microsoft ActiveX Data objects 2.6 Library  
    
  5. Eseguire l'applicazione.

Questo è lo schema XDR equivalente:

<?xml version="1.0" ?>  
<Schema xmlns="urn:schemas-microsoft-com:xml-data"   
        xmlns:dt="urn:schemas-microsoft-com:xml:datatypes"    
        xmlns:sql="urn:schemas-microsoft-com:xml-sql" >  
  
   <ElementType name="CustomerID" dt:type="int" />  
   <ElementType name="CompanyName" dt:type="string" />  
   <ElementType name="City" dt:type="string" />  
  
   <ElementType name="root" sql:is-constant="1">  
      <element type="Customers" />  
   </ElementType>  
  
   <ElementType name="Customers" sql:relation="Cust"  >  
      <element type="CustomerID" sql:field="CustomerID" />  
      <element type="CompanyName" sql:field="CompanyName" />  
      <element type="City" sql:field="City" />  
      <element type="Order" >  
         <sql:relationship  
                key-relation="Cust"  
                key="CustomerID"  
                foreign-key="CustomerID"  
                foreign-relation="CustOrder" />  
      </element>  
   </ElementType>  
    <ElementType name="Order" sql:relation="CustOrder" >  
      <AttributeType name="OrderID" />  
      <AttributeType name="CustomerID" />  
      <attribute type="OrderID" />  
      <attribute type="CustomerID" />  
    </ElementType>  
</Schema>  

J. Caricamento bulk nelle colonne del tipo di dati xml

Se lo schema di mapping specifica una colonna di tipo di dati xml utilizzando l'annotazione sql:datatype="xml" , il caricamento bulk XML può copiare gli elementi figlio XML per il campo mappato dal documento di origine in questa colonna.

Si consideri lo schema XSD seguente, che esegue il mapping di una vista della tabella Production.ProductModel nel database di esempio AdventureWorks. In questa tabella il campo CatalogDescription del tipo di xml dati viene mappato a un <elemento Desc> usando le sql:field annotazioni e sql:datatype="xml" .

<?xml version="1.0" encoding="utf-8" ?>  
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
           xmlns:sql="urn:schemas-microsoft-com:mapping-schema"  
           xmlns="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">   
  <xsd:element name="ProductModel"  sql:relation="Production.ProductModel" >  
    <xsd:complexType>  
      <xsd:sequence>  
        <xsd:element name="Name" type="xs:string"></xsd:element>  
        <xsd:element name="Desc" sql:field="CatalogDescription" sql:datatype="xml">  
        <xsd:complexType>  
          <xsd:sequence>  
            <xsd:element name="ProductDescription">  
              <xsd:complexType>  
                <xsd:sequence>  
                  <xsd:element name="Summary" type="xs:anyType"/>  
                </xsd:sequence>  
              </xsd:complexType>  
            </xsd:element>  
          </xsd:sequence>  
        </xsd:complexType>  
        </xsd:element>   
     </xsd:sequence>  
     <xsd:attribute name="ProductModelID" sql:field="ProductModelID" />  
    </xsd:complexType>  
  </xsd:element>  
</xsd:schema>  

Per testare un esempio funzionante

  1. Verificare che il database di esempio AdventureWorks sia installato.

  2. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleSchema.xml. Copiare lo schema XSD sopra e incollarlo nel file e salvarlo.

  3. Creare un file nell'editor di testo o XML preferito e salvarlo come SampleXMLData.xml. Copiare il documento XML seguente e incollarlo nel file e salvarlo nella stessa cartella usata per il passaggio precedente.

    <ProductModel ProductModelID="2005">  
        <Name>Mountain-100 (2005 model)</Name>  
        <Desc><?xml-stylesheet href="ProductDescription.xsl" type="text/xsl"?>  
            <p1:ProductDescription xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription"   
                  xmlns:wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain"   
                  xmlns:wf="https://www.adventure-works.com/schemas/OtherFeatures"   
                  xmlns:html="http://www.w3.org/1999/xhtml"   
                  xmlns="">  
                <p1:Summary>  
                    <html:p>Our top-of-the-line competition mountain bike.   
          Performance-enhancing options include the innovative HL Frame,   
          super-smooth front suspension, and traction for all terrain.  
                            </html:p>  
                </p1:Summary>  
                <p1:Manufacturer>  
                    <p1:Name>AdventureWorks</p1:Name>  
                    <p1:Copyright>2002-2005</p1:Copyright>  
                    <p1:ProductURL>HTTP://www.Adventure-works.com</p1:ProductURL>  
                </p1:Manufacturer>  
                <p1:Features>These are the product highlights.   
                     <wm:Warranty>  
                        <wm:WarrantyPeriod>3 years</wm:WarrantyPeriod>  
                        <wm:Description>parts and labor</wm:Description>  
                    </wm:Warranty><wm:Maintenance>  
                        <wm:NoOfYears>10 years</wm:NoOfYears>  
                        <wm:Description>maintenance contract available through your dealer or any AdventureWorks retail store.</wm:Description>  
                    </wm:Maintenance><wf:wheel>High performance wheels.</wf:wheel><wf:saddle>  
                        <html:i>Anatomic design</html:i> and made from durable leather for a full-day of riding in comfort.</wf:saddle><wf:pedal>  
                        <html:b>Top-of-the-line</html:b> clipless pedals with adjustable tension.</wf:pedal><wf:BikeFrame>Each frame is hand-crafted in our Bothell facility to the optimum diameter   
          and wall-thickness required of a premium mountain frame.   
          The heat-treated welded aluminum frame has a larger diameter tube that absorbs the bumps.</wf:BikeFrame><wf:crankset> Triple crankset; alumunim crank arm; flawless shifting. </wf:crankset></p1:Features>  
                <!-- add one or more of these elements... one for each specific product in this product model -->  
                <p1:Picture>  
                    <p1:Angle>front</p1:Angle>  
                    <p1:Size>small</p1:Size>  
                    <p1:ProductPhotoID>118</p1:ProductPhotoID>  
                </p1:Picture>  
                <!-- add any tags in <specifications> -->  
                <p1:Specifications> These are the product specifications.  
                       <Material>Almuminum Alloy</Material><Color>Available in most colors</Color><ProductLine>Mountain bike</ProductLine><Style>Unisex</Style><RiderExperience>Advanced to Professional riders</RiderExperience></p1:Specifications>  
            </p1:ProductDescription>  
        </Desc>  
    </ProductModel>  
    
  4. Creare un file nell'editor di testo o XML preferito e salvarlo come BulkloadXml.vbs. Copiare il codice VBScript seguente e incollarlo nel file. Salvarlo nella stessa cartella utilizzata per i file di dati e schema XML precedenti.

    set objBL = CreateObject("SQLXMLBulkLoad.SQLXMLBulkload.4.0")  
    objBL.ConnectionString = "provider=SQLOLEDB;data source=MyServer;database=AdventureWorks;integrated security=SSPI"  
    
    Dim fso, sAppPath  
    Set fso = CreateObject("Scripting.FileSystemObject")   
    sAppPath = fso.GetFolder(".")   
    
    objBL.ErrorLogFile = sAppPath & "\error.log"  
    
    'Execute XML bulkload using file.  
    objBL.Execute "SampleSchema.xml", "SampleXMLData.xml"  
    set objBL=Nothing  
    
  5. Eseguire lo script BulkloadXml.vbs.