ADO を使用した、アップデートグラムの実行 (SQLXML 4.0)
この Microsoft Visual Basic アプリケーションでは、ADO を使用して Microsoft SQL Server インスタンスへの接続を確立した後、アップデートグラムを実行します。このアップデートグラムでは、特定の従業員の名前が更新されます。この例では、SQL Server 2005 のサンプル データベース AdventureWorks を使用します。
サンプル アプリケーションの処理は次のとおりです。
- conn オブジェクト (ADODB.Connection) から、特定のサーバー コンピュータで実行されている SQL Server インスタンスへの接続が確立されます。
- 確立された接続で、cmd オブジェクト (ADODB.Command) が実行されます。
- コマンド言語が DBGUID_MSSQLXML に設定されます。
- アップデートグラムがコマンド ストリーム (strmIn) にコピーされます。
- 返されるデータを受信するため、コマンドの出力ストリームが StrmOut オブジェクト (ADODB.Stream) に設定されます。
- 最後にコマンド (アップデートグラム) が実行されます。
次にサンプル コードを示します。
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
メモ : |
---|
ADO から SQLXML を使用して、XSD スキーマを指定するアップデートグラムを実行する場合は、次のコード例のように、接続オブジェクトで "SQLXML Version " プロパティを "SQLXML.4.0 " に設定する必要があります。 |
conn.Properties("SQLXML Version") = "SQLXML.4.0"
アップデートグラムのマッピング スキーマの指定
この例では、アップデートグラムでマッピング スキーマを指定し、それを使用する方法を示します。
次の XSD スキーマ (EmpSchema.xml) をディスクに保存し、コードで指定されているパスを、コンピュータ上のマッピング スキーマの場所に変更します。このコードでは、スキーマが C: ドライブの Schemas フォルダに保存されていることを想定しています。
<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>
XSD と XDR スキーマはどちらも指定できます。XDR スキーマの場合は次のようになります。
<?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>
これは、マッピング スキーマが関連付けられているアップデートグラムを実行する Visual Basic コードです。このアップデートグラムでは、Person.Contact テーブルにある連絡先 (Contact=1) のミドル ネームが更新されます。
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
パラメータの引き渡し
上の Visual Basic アプリケーションでは、パラメータを渡しません。このアプリケーションでは、ContactID 値と MiddleName 値を、パラメータ化された入力としてアップデートグラムに渡します。
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