tailieunhanh - Apress-Visual CSharp 2010 Recipes A Problem Solution Approach_4

Ví dụ sau đây cho thấy làm thế nào để lấy kết quả như XML bằng cách sử dụng mệnh đề FOR XML và phương pháp ExecuteXmlReader: sử dụng sử dụng sử dụng sử dụng hệ thống; ; ; ; | CHAPTER 9 DATABASE ACCESS The Code The following example demonstrates how to retrieve results as XML using the FOR XML clause and the ExecuteXmlReader method using System using using using namespace J . class Recipe09_08 . public static void ConnectedExample Create a new SqlConnection object. using SqlConnection con new SqlConnection Configure the SqlConnection object s connection string. @ Data Source . sqlexpress Database Northwind Integrated Security SSPI Create and configure a new command that includes the FOR XML AUTO clause. using SqlCommand com 1 . Ill com. CommandText SELECT CustomerID CompanyName FROM Customers FOR XML AUTO Open the database connection. con. Open Execute the command and retrieve an XmlReader to access the results. using XmlReader reader - while Element if . . . for int i 0 i i . 1 . .1 i 0 1 450 CHAPTER 9 DATABASE ACCESS Move the XmlReader back to the element node. public static void DisconnectedExample XmlDocument doc new XmlDocument Create a new SqlConnection object. using SqlConnection con new SqlConnection Configure the SqlConnection object s connection string. @ Data Source . sqlexpress Database Northwind Integrated Security SSPI Create and configure a new command that includes the FOR XML AUTO clause. SqlCommand com com. CommandText SELECT CustomerID CompanyName FROM Customers FOR XML AUTO Open the database connection. con. Open Load the XML data into the XmlDocument. Must first create a root element into which to place each result row element. XmlReader reader

TỪ KHÓA LIÊN QUAN