--- title: "Executing an Updategram by Using ADO (SQLXML 4.0) | Microsoft Docs" ms.custom: "" ms.date: "03/06/2017" ms.prod: "sql-server-2014" ms.reviewer: "" ms.technology: xml ms.topic: "reference" helpviewer_keywords: - "ADO [SQLXML]" - "updategrams [SQLXML], ADO" - "executing updategrams [SQLXML]" ms.assetid: 78610ca0-f763-45fc-ac64-da5c192cc3e5 author: MightyPen ms.author: genemi manager: craigg --- # Executing an Updategram by Using ADO (SQLXML 4.0) This [!INCLUDE[msCoName](../../../includes/msconame-md.md)] Visual Basic application uses ADO to establish a connection to an instance of Microsoft [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] and execute an updategram. The updategram updates the last name of a specific employee. This example uses the AdventureWorks sample database. In this sample application: - The **conn** object (**ADODB.Connection**) establishes a connection to a running instance of [!INCLUDE[ssNoVersion](../../../includes/ssnoversion-md.md)] on a specific server computer. - The **cmd** object (**ADODB.Command**) executes on the established connection. - The command dialect is set to DBGUID_MSSQLXML. - The updategram is copied to the command stream (**strmIn**). - The command's output stream is set to the **StrmOut** object (**ADODB.Stream**) to receive any returned data. - Finally the command (updategram) is executed. Here is the sample code: ```vb 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 = "" SQLxml = SQLxml & " " SQLxml = SQLxml & " " SQLxml = SQLxml & " " SQLxml = SQLxml & " " SQLxml = SQLxml & " " SQLxml = SQLxml & " " SQLxml = SQLxml & " " SQLxml = SQLxml & " " SQLxml = SQLxml & "" ' 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 ``` > [!NOTE] > If you're using SQLXML from ADO to execute updategrams that specify an XSD schema, you must set the "`SQLXML Version`" property to "`SQLXML.4.0`" on the connection object, as shown in the following example line of code: > > `conn.Properties("SQLXML Version") = "SQLXML.4.0"` ## Specifying a Mapping Schema for the Updategram This example illustrates how to specify and use a mapping schema in an updategram. Save the following XSD schema (EmpSchema.xml) to your disk, and be sure to update the path that is specified in the code to the location of the mapping schema on your disk. The code assumes that the schema is saved on the C: drive in the Schemas folder. ``` ``` Because both XSD and XDR schemas can be specified, this is the equivalent XDR schema: ``` ``` This is the Visual Basic code to execute an updategram that has an associated mapping schema. The updategram updates the middle name for contact 1 in the Person.Contact table. ```vb 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 "" strmIn.WriteText " " strmIn.WriteText " " strmIn.WriteText " " strmIn.WriteText " " strmIn.WriteText " " strmIn.WriteText " " strmIn.WriteText " " strmIn.WriteText " " strmIn.WriteText "" ' 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 ``` ## Passing Parameters In the Visual Basic applications provided earlier, parameters are not passed. In this application, the **ContactID** and **MiddleName** values are passed as parameterized input to the updategram. ```vb 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 = "" SQLxml = SQLxml & "" SQLxml = SQLxml & "" SQLxml = SQLxml & "" SQLxml = SQLxml & "" SQLxml = SQLxml & "" SQLxml = SQLxml & " " SQLxml = SQLxml & " " SQLxml = SQLxml & "" SQLxml = SQLxml & "" SQLxml = SQLxml & "" SQLxml = SQLxml & "" SQLxml = SQLxml & "" SQLxml = SQLxml & "" ' 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 ```