12.4 Retrieve XML from SQL Server 2000 Sometimes I have to pull data from my SQL Server database into an XML document format.. Technique To accomplish this task, you will create a Comma
Trang 112.4 Retrieve XML from SQL Server 2000
Sometimes I have to pull data from my SQL Server database into an XML document format How do I do that with SQL Server 2000?
Technique
To accomplish this task, you will create a Command object with the Transact-SQL
SELECT statement that you want to execute However, at the end of your SQL statement, you will add the clause FOR XML mode The mode can be any of these that are listed:
• RAW With this mode, each of the rows that is returned in the query result is made into a generic XML element with <row /> as the identifier tag
• AUTO Each of the rows and the columns are identified with tags for each of the
elements and attributes, using the column names as identifier tags
• EXPLICIT Here, you have to nest and create your query in a particular way For
more information on this mode, check out the SQL Server Books Online
For this example, the code will use the RAW mode and look like this:
SELECT * FROM Customers FOR XML RAW
To execute the SQL statement in this case, you use the method ExecuteXMLReader When you use this method, an XMLReader is returned You should then iterate through the elements as seen in How-To 12.2
Steps
Open and run the Visual Basic NET-Chapter 12 solution From the main Web page, click on the hyperlink with the caption How-To 12.4: Retrieving XML from SQL Server
2000 When the page loads, you will see an example of a T-SQL statement that retrieves data from SQL Server 2000 in an XML format Click on the button labeled Retrieve XML, and the data will be listed in the TextArea at the bottom of the form (see Figure 12.4)
1 Create a Web Form Then place the Label, TextBox, and Button objects as seen in Figure 12.4 on the form with the properties in Table 12.8 set
Table 12.8 Label, TextBox, and Button Control Property Settings
Object Property Setting
Trang 2Label Text SQL To Execute
ELEMENTS
NavigateURL wfrmMain.aspx
2 Add the code in Listing 12.10 to the Click event of btnRetrieveXML Taking the SQL statement displayed in the "Technique" section, the Command object
cmdCust is created, and ExecuteXMLReader is invoked The XMLReader then iterates through each of the elements in the document, and they concatenate to a string Last, the string is assigned to the InnerText property of taOutput, and the connection to the XMLReader object is closed
Listing 12.10 wfrmHowTo12_4.aspx.vb: Reading an XML Document Using XMLTextReader
Private Sub btnRetrieveXML_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnRetrieveXML.Click
Dim cnn As New SqlClient.SqlConnection(BuildCnnStr("(local)",
"Northwind"))
Dim cmdCust As SqlClient.SqlCommand = New SqlClient.SqlCommand( _ Me.txtSQLToExecute.Text, cnn)
Dim xrCust As System.Xml.XmlReader
Dim intAtts As Int32
Dim intCurrAtt As Int32
Dim strOut As String
Try
cnn.Open()
xrCust = cmdCust.ExecuteXmlReader()
Trang 3While xrCust.Read()
intAtts = xrCust.AttributeCount
If xrCust.NodeType <> System.Xml.XmlNodeType.XmlDeclaration Then
If intAtts > 0 Then
For intCurrAtt = 0 To intAtts - 1
strOut &= xrCust(intCurrAtt) & vbCrLf
Next
Else
strOut &= xrCust.Value & vbCrLf
End If
End If
End While
Catch excp As Exception
strOut &= "Following Error Occurred: " & excp.Message
Finally
strOut &= vbCrLf & "Done Processing "
taOutput.InnerText = strOut
If Not xrCust Is Nothing Then
xrCust.Close()
End If
cnn.Close()
End Try
End Sub
Note
You have been using the BuildCnnStr() function throughout this book You should add this function to a module or copy it from other chapters Here is the code for the function:
Function BuildCnnStr(ByVal strServer As String,
ByVal strDatabase As String) As String
Trang 4Dim strTemp As String strTemp = "Data Source=" & strServer & ";"
strTemp &= "Initial Catalog=" & strDatabase & ";"
strTemp &= "Integrated Security=SSPI"
Return strTemp End Function
Figure 12.4 The information displayed here was read from SQL Server in an XML
format
Comments
Normally, you would be taking the XML document that the command object returned and passing that on to another system that requires the data to be in XML format The data was displayed from the XMLReader for demonstration purposes