Sunday, June 17, 2012

Contoh Kode XML Query XPath

Option Explicit

Dim gCn As New ADODB.Connection

Const DBGUID_DEFAULT As String = "{C8B521FB-5CF3-11CE-ADE5-00AA0044773D}"
Const DBGUID_SQL As String = "{C8B522D7-5CF3-11CE-ADE5-00AA0044773D}"
Const DBGUID_MSSQLXML As String = "{5D531CB2-E6Ed-11D2-B252-00C04F681B71}"
Const DBGUID_XPATH As String = "{ec2a4293-e898-11d2-b1b7-00c04f680c56}"

Private Sub cmdExitProgram_Click()
Unload Me
End
End Sub

Private Sub cmdTestIt_Click()

Dim cmd As ADODB.Command
Dim strm As ADODB.Stream

On Error GoTo trap
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = gCn

Set strm = New ADODB.Stream
strm.Open
cmd.Dialect = DBGUID_XPATH

cmd.Properties("Mapping Schema") = App.Path & "\CustomerOrder.xdr"
cmd.Properties("Output Stream") = strm

txtXPath = Trim(txtXPath)
If txtXPath = "" Then
txtXPath = "Customers"
End If

cmd.CommandText = txtXPath
cmd.Execute , , adExecuteStream
strm.Position = 0
txtResults = strm.ReadText
txtResults = Replace(txtResults, "><", ">" & vbCrLf & "<")
strm.Position = 0
strm.Close

GoTo cleanup

trap:

MsgBox "Error (" & Err.Number & ") -- " & Err.Description

cleanup:
Set strm = Nothing
Set cmd = Nothing

Exit Sub

End Sub

Private Sub Form_Load()

On Error GoTo trap
Set gCn = New ADODB.Connection
gCn.ConnectionString = "PROVIDER=SQLOLEDB;Data Source=.;Initial Catalog=Northwind;uid=sa;pwd="
gCn.Open
Exit Sub
trap:
MsgBox "Failed to connect to database. Program Shutting down."
Unload Me
End
End Sub