Working with the xml Data Type in Applications
This topic describes the options that are available to you for working with the xml data type in your application. The topic includes information about the following:
Handling XML from an xml type column by using ADO and SQL Native Client
Handling XML from an xml type column by using ADO.NET
Handling xml type in parameters by using ADO.NET
In SQL Server 2005, you can continue to use MDAC components for data access to the types and features that are provided in SQL Server 2000 without having to make code changes. However, so that you can access the types and features that are new to SQL Server 2005, a new initialization property, DataTypeCompatibility, must be set in the ADO connection string.
For example, the following Visual Basic Scripting Edition (VBScript) sample shows the results of querying an xml data type column,
Demographics, in the
Sales.Store table of the
AdventureWorks sample database. Specifically, the query looks for the instance value of this column for the row where the
CustomerID is equal to
Const DS = "MyServer" Const DB = "AdventureWorks" Set objConn = CreateObject("ADODB.Connection") Set objRs = CreateObject("ADODB.Recordset") CommandText = "SELECT Demographics" & _ " FROM Sales.Store" & _ " WHERE CustomerID = 3" & _ " OR CustomerID = 4" ConnectionString = "Provider=SQLNCLI" & _ ";Data Source=" & DS & _ ";Initial Catalog=" & DB & _ ";Integrated Security=SSPI;" & _ "DataTypeCompatibility=80" 'Connect to the data source. objConn.Open ConnectionString 'Execute command through the connection and display Set objRs = objConn.Execute(CommandText) Dim rowcount rowcount = 0 Do While Not objRs.EOF rowcount = rowcount + 1 MsgBox "Row " & rowcount & _ vbCrLf & vbCrLf & objRs(0) objRs.MoveNext Loop 'Clean up. objRs.Close objConn.Close Set objRs = Nothing Set objConn = Nothing
This example shows how to set the data type compatibility property. By default, this is set to 0 when you are using SQL Native Client. After you set the value to 80, the SQL Native Client provider will make xml and user-defined type columns appear as SQL Server 2000 data types. This would be DBTYPE_WSTR and DBTYPE_BYTES, respectively.
SQL Native Client must also be installed on the client computer and the connection string must specify it for use as the data provider with "
Verify that the SQL Native Client is installed and that MDAC 2.version 6.0, or a subsequent version, is available on the client computer.
For more information, see SQL Native Client Programming.
Verify that the AdventureWorks sample database in SQL Server 2005 is installed.
This example requires the AdventureWorks sample database. For more information, see Installing AdventureWorks Sample Databases and Samples.
Copy the code shown previously in this topic and paste the code into your text or code editor. Save the file as HandlingXmlDataType.vbs.
Modify the script as required for your SQL Server 2005 installation and save your changes.
For example, where
MyServeris specified, you should replace it with either
(local)or the actual name of the server on which SQL Server 2005 is installed.
Run HandlingXmlDataType.vbs and execute the script.
The results should be similar to the following sample output:
Row 1 <StoreSurvey xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/StoreSurvey"> <AnnualSales>1500000</AnnualSales> <AnnualRevenue>150000</AnnualRevenue> <BankName>Primary International</BankName> <BusinessType>OS</BusinessType> <YearOpened>1974</YearOpened> <Specialty>Road</Specialty> <SquareFeet>38000</SquareFeet> <Brands>3</Brands> <Internet>DSL</Internet> <NumberEmployees>40</NumberEmployees> </StoreSurvey> Row 2 <StoreSurvey xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/StoreSurvey"> <AnnualSales>300000</AnnualSales> <AnnualRevenue>30000</AnnualRevenue> <BankName>United Security</BankName> <BusinessType>BM</BusinessType> <YearOpened>1976</YearOpened> <Specialty>Road</Specialty> <SquareFeet>6000</SquareFeet> <Brands>2</Brands> <Internet>DSL</Internet> <NumberEmployees>5</NumberEmployees> </StoreSurvey>
To handle XML from an xml data type column by using ADO.NET and the Microsoft .NET Framework you can use the standard behavior of the SqlCommand class. For example, an xml data type column and its values can be retrieved in the same way any SQL column is retrieved by using a SqlDataReader.However, if you want to work with the contents of an xml data type column as XML, you will first have to assign the contents to an XmlReader type.
For more information and example code, see "XML Column Values in a Data Reader" in the Microsoft .NET Framework 2.0 SDK documentation.
To handle an xml data type passed as a parameter in ADO.NET and the .NET Framework, you can supply the value as an instance of the SqlXml data type. No special handling is involved, because xml data type columns in SQL Server 2005 can accept parameter values in the same way as other columns and data types, such as string or integer.
For more information and example code, see "XML Values as Command Parameters" in the Microsoft .NET Framework 2.0 SDK documentation.