Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (11.68 KB, 2 trang )
[ Team LiB ]
Recipe 8.5 Reading XML Data Directly from SQL Server
Problem
You need to read XML data directly from the SQL Server.
Solution
Use the FOR XML clause in the stored procedure or SQL statement.
The C# code is shown in Example 8-8
.
Example 8-8. File: ReadXmlDirectForm.cs
// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
// . . .
// Select statement to read XML directly.
String sqlText = "SELECT * FROM Orders FOR XML AUTO, XMLDATA";
// Create the connection.
SqlConnection conn = new SqlConnection(
ConfigurationSettings.AppSettings["Sql_ConnectString"]);
conn.Open( );
// Create the command.
SqlCommand cmd = new SqlCommand(sqlText, conn);
// Read the XML data into a XML reader.