Freigeben über


Ausführen eines Updategrams mit ADO (SQLXML 4.0)

Diese Microsoft Visual Basic-Anwendung verwendet ADO, um eine Verbindung mit einer Instanz von Microsoft SQL Server herzustellen und ein Updategram auszuführen. Das Updategram aktualisiert den Nachnamen eines bestimmten Mitarbeiters. In diesem Beispiel wird die AdventureWorks-Musterdatenbank verwendet.

Diese Beispielanwendung zeigt Folgendes:

  • Das conn-Objekt (ADODB.Connection) stellt eine Verbindung zu einer aktuell ausgeführten Instanz von SQL Server auf einem bestimmten Servercomputer her.

  • Das cmd -Objekt (ADODB.Command) wird für die hergestellte Verbindung ausgeführt.

  • Der Befehlsdialekt wird auf DBGUID_MSSQLXML festgelegt.

  • Das Updategram wird in den Befehlsdatenstrom (strmIn) kopiert.

  • Der Ausgabedatenstrom des Befehls wird auf das StrmOut-Objekt (ADODB.Stream) festgelegt, um eventuell zurückgegebene Daten zu empfangen.

  • Schließlich wird der Befehl (Updategram) ausgeführt.

Der Beispielcode sieht wie folgt aus:

Private Sub Form_Load()

  Dim cmd As New ADODB.Command
  Dim conn As New ADODB.Connection
  Dim strmIn As New ADODB.Stream
  Dim strmOut As New ADODB.Stream
  Dim SQLxml As String

  ' Open a connection to the instance of SQL Server.
  conn.Provider = "SQLOLEDB"
  conn.Open "server=(local); database=AdventureWorks; Integrated Security=SSPI; "
  conn.Properties("SQLXML Version") = "SQLXML.4.0"
  Set cmd.ActiveConnection = conn

  ' Build the command string in the form of an XML template.
    SQLxml = "<ROOT xmlns:updg='urn:schemas-microsoft-com:xml-updategram' >"
    SQLxml = SQLxml & "  <updg:sync updg:nullvalue='IsNULL'>"
    SQLxml = SQLxml & "    <updg:before>"
    SQLxml = SQLxml & "       <Person.Contact ContactID='64' Title='IsNULL'/>"
    SQLxml = SQLxml & "    </updg:before>"
    SQLxml = SQLxml & "    <updg:after>"
    SQLxml = SQLxml & "       <Person.Contact ContactID='64' Title='Mr.'/>"
    SQLxml = SQLxml & "    </updg:after>"
    SQLxml = SQLxml & "  </updg:sync>"
    SQLxml = SQLxml & "</ROOT>"

  ' Set the command dialect to DBGUID_MSSQLXML.
  cmd.Dialect = "{5d531cb2-e6ed-11d2-b252-00c04f681b71}"

  ' Open the command stream and write our template to it.
  strmIn.Open
  strmIn.WriteText SQLxml
  strmIn.Position = 0

  Set cmd.CommandStream = strmIn

  ' Execute the command, open the return stream, and read the result.
  strmOut.Open
  strmOut.LineSeparator = adCRLF
  cmd.Properties("Output Stream").Value = strmOut
  cmd.Properties("Output Encoding").Value = "UTF-8"
  cmd.Execute , , adExecuteStream
  strmOut.Position = 0
  Debug.Print strmOut.ReadText
  strmOut.Close
  strmIn.Close

End Sub
HinweisHinweis

Wenn Sie SQLXML von ADO zur Ausführung von Updategrams verwenden, die ein XSD-Schema angeben, müssen Sie die "SQLXML Version"-Eigenschaft auf dem Verbindungsobjekt auf "SQLXML.4.0" einstellen, wie in der folgenden Beispielcodezeile dargestellt:

conn.Properties("SQLXML Version") = "SQLXML.4.0"

Angeben eines Zuordnungsschemas für das Updategram

In diesem Beispiel wird veranschaulicht, wie ein Zuordnungsschema in einem Updategram angegeben und verwendet wird.

Speichern Sie das folgende XSD-Schema (EmpSchema.xml) auf der Festplatte, und ändern Sie den im Code angegebenen Pfad entsprechend, damit er auf Ihr Zuordnungsschema verweist. Im Code wird davon ausgegangen, dass das Schema im Laufwerk C im Ordner Schemas gespeichert ist.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
  <xsd:element name="Contact" sql:relation="Person.Contact" >
   <xsd:complexType>
        <xsd:attribute name="CID"  
                       sql:field="ContactID" 
                       type="xsd:string" /> 
        <xsd:attribute name="MName"  
                       sql:field="MiddleName"  
                       type="xsd:string" />
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Da sowohl XSD als auch XDR-Schemas angegeben werden können, folgt an dieser Stelle das entsprechende XDR-Schema:

<?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="Contact" sql:relation="Person.Contact" >
       <AttributeType name="CID" />
       <AttributeType name="MName" />

       <attribute type="CID" sql:field="ContactID" />
       <attribute type="MName" sql:field="MiddleName" />
     </ElementType>
   </Schema> 

Dies ist der Visual Basic-Code für die Ausführung eines Updategrams, dem ein Zuordnungsschema zugewiesen ist. Das Updategram aktualisiert den zweiten Vornamen für Kontakt 1 in der Tabelle Person.Contact.

Private Sub Form_Load()
    Dim cmd As New ADODB.Command
    Dim conn As New ADODB.Connection
    Dim strmIn As New ADODB.Stream
    Dim strmOut As New ADODB.Stream

    ' Open a connection to the SQL Server.
    conn.Provider = "SQLOLEDB"
    conn.Open "server=(local); database=AdventureWorks; Integrated Security='SSPI' ;"
    conn.Properties("SQLXML Version") = "SQLXML.4.0"
    Set cmd.ActiveConnection = conn
    
    ' Open the command stream and write the template to it.
    strmIn.Open
    strmIn.WriteText "<ROOT xmlns:updg='urn:schemas-microsoft-com:xml-updategram' >"
    strmIn.WriteText "  <updg:sync mapping-schema='C:\Schemas\EmpSchema.xml' >"
    strmIn.WriteText "      <updg:before>"
    strmIn.WriteText "          <Contact CID='1' />"
    strmIn.WriteText "      </updg:before>"
    strmIn.WriteText "      <updg:after>"
    strmIn.WriteText "          <Contact MName='M.'/>"
    strmIn.WriteText "      </updg:after>"
    strmIn.WriteText "  </updg:sync>"
    strmIn.WriteText "</ROOT>"
    
    ' Set the command dialect to XML.
    cmd.Dialect = "{5d531cb2-e6ed-11d2-b252-00c04f681b71}"
    strmIn.Position = 0
    Set cmd.CommandStream = strmIn
 
    ' Execute the command, open the return stream, and read the result.
    strmOut.Open
    strmOut.LineSeparator = adCRLF
    cmd.Properties("Output Stream").Value = strmOut
    cmd.Execute , , adExecuteStream
    strmOut.Position = 0
    Debug.Print strmOut.ReadText
    strmOut.Close
    strmIn.Close
    conn.Close
End Sub

Übergeben von Parametern

In den früher bereitgestellten Visual Basic-Anwendungen wurden Parameter nicht übergeben. In dieser Anwendung werden der ContactID-Wert und der MiddleName-Wert als parametrisierte Eingabe an das Updategram übergeben.

Private Sub Form_Load()
  
  Dim cmd As New ADODB.Command
  Dim conn As New ADODB.Connection
  Dim strmIn As New ADODB.Stream
  Dim strmOut As New ADODB.Stream
  Dim InputContactID As String
  Dim InputMiddleName As String

  InputContactID = "1"
  InputMiddleName = "Q."

  ' Open a connection to the instance of SQL Server.
  conn.Provider = "SQLOLEDB"
  conn.Open "server=(local); database=AdventureWorks; Integrated Security=SSPI; "
  conn.Properties("SQLXML Version") = "SQLXML.4.0"
  Set cmd.ActiveConnection = conn

  ' Build the command string in the form of an XML template.
  SQLxml = "<ROOT xmlns:updg='urn:schemas-microsoft-com:xml-updategram' >"
  SQLxml = SQLxml & "<updg:header>"
  SQLxml = SQLxml & "<updg:param name='ContactID'/>"
  SQLxml = SQLxml & "<updg:param name='MiddleName' />"
  SQLxml = SQLxml & "</updg:header>"
  SQLxml = SQLxml & "<updg:sync >"
  SQLxml = SQLxml & " <updg:before>"
  SQLxml = SQLxml & "   <Person.Contact ContactID='$ContactID' />"
  SQLxml = SQLxml & "</updg:before>"
  SQLxml = SQLxml & "<updg:after>"
  SQLxml = SQLxml & "<Person.Contact MiddleName='$MiddleName' />"
  SQLxml = SQLxml & "</updg:after>"
  SQLxml = SQLxml & "</updg:sync>"
  SQLxml = SQLxml & "</ROOT>"

  ' Set the command dialect to XML.
  cmd.Dialect = "{5d531cb2-e6ed-11d2-b252-00c04f681b71}"

  ' Open the command stream and write the template to it.
  strmIn.Open
  strmIn.WriteText SQLxml
  strmIn.Position = 0

  Set cmd.CommandStream = strmIn

  ' Execute the command, open the return stream, and read the result.
  strmOut.Open
  strmOut.LineSeparator = adCRLF
  cmd.NamedParameters = True
  cmd.Parameters.Append cmd.CreateParameter("@ContactID", adBSTR, adParamInput, 1, InputContactID)
  cmd.Parameters.Append cmd.CreateParameter("@MiddleName", adBSTR, adParamInput, 7, InputMiddleName)
  cmd.Properties("Output Stream").Value = strmOut
  cmd.Execute , , adExecuteStream
  strmOut.Position = 0
  Debug.Print strmOut.ReadText

End Sub