Danh mục

Reading XML Data Directly from SQL Server

Số trang: 2      Loại file: pdf      Dung lượng: 11.68 KB      Lượt xem: 16      Lượt tải: 0    
Hoai.2512

Phí tải xuống: miễn phí Tải xuống file đầy đủ (2 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

[ 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
Nội dung trích xuất từ tài liệu:
Reading XML Data Directly from SQL Server[ Team LiB ]Recipe 8.5 Reading XML Data Directly from SQL ServerProblemYou need to read XML data directly from the SQL Server.SolutionUse 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 constantsusing 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.XmlReader xr = cmd.ExecuteXmlReader( );// Read the data from the XML reader into the DataSet.DataSet ds = new DataSet( );ds.ReadXml(xr, XmlReadMode.Fragment);xr.Close( );conn.Close( );xmlTextBox.Text = ds.GetXml( );DiscussionSQL Server 2000 introduced support for retrieving data in XML format using the FORXML clause. The .NET SQL Server data provider SqlCommand object has anExecuteXmlReader( ) that allows you to retrieve an XML stream directly from SQLServer, where it returns an XmlReader that contains the results of the SQL query. TheExecuteXmlReader( ) method can only be used with SQL statements that return XMLdata, such as those with a FOR XML clause. The ExecuteXmlReader( ) method can alsobe used to return ntext data containing valid XML.For more information about the FOR XML clause, see Microsoft SQL Server BooksOnline.[ Team LiB ]

Tài liệu được xem nhiều:

Gợi ý tài liệu liên quan: