• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            Obtain an XML Document from a SQL Server Query(AOD.NET 七)

            Problem

            You need to execute a query against a SQL Server 2000 or MSDE database and retrieve the results as XML.

            Solution

            Specify the FOR XML clause on your SQL query to return the results as XML. Execute the command using the SqlCommand.ExecuteXmlReader method, which returns a System.Xml.XmlReader object through which you can access the returned XML data.

            Discussion

            SQL Server 2000 and MSDE provide direct support for XML. You simply need to add the clause FOR XML AUTO to the end of a SQL query to indicate that the results should be returned as XML. By default, the XML representation is not a full XML document. Instead, it simply returns the result of each record in a separate element, with all the fields as attributes. For example, the query

            SELECT CustomerID, CompanyName FROM Customers FOR XML AUTO

            returns XML with the following structure:

            <Customers CustomerID="ALFKI" CompanyName="Alfreds Futterkiste"/>
            <Customers CustomerID="ANTON" CompanyName="Antonio Moreno Taquería"/>
            <Customers CustomerID="GOURL" CompanyName="Gourmet Lanchonetes"/>
            §

            Alternatively, you can add the ELEMENTS keyword to the end of a query to structure the results using nested elements rather than attributes. For example, the query

            SELECT CustomerID, CompanyName FROM Customers FOR XML AUTO, ELEMENTS

            returns XML with the following structure:

            <Customers>
              <CustomerID>ALFKI</CustomerID>
              <CompanyName>Alfreds Futterkiste</CompanyName>
            </Customers>
            <Customers>
              <CustomerID>ANTON</CustomerID>
              <CompanyName>Antonio Moreno Taquería</CompanyName>
            </Customers>
            <Customers>
              <CustomerID>GOURL</CustomerID>
              <CompanyName>Gourmet Lanchonetes</CompanyName>
            </Customers>
            §
            Note 

            You can also fine-tune the format in more detail using the FOR XML EXPLICIT syntax. For example, this allows you to convert some fields to attributes and others to elements. Refer to the SQL Server Books Online for more information.

            The following example demonstrates how to retrieve results as XML using the FOR XML clause and the ExecuteXmlReader method. Notice that the connection can't be used for any other commands while the XmlReader is open. You should process the results as quickly as possible and must always close the XmlReader. (Chapter 5 contains more detailed examples of how to use the XmlReader class.)

            using System;
            using System.Xml;
            using System.Data;
            using System.Data.SqlClient;
            
            public class XmlQueryExample {
            
                public static void Main() {
            
                    // Create a new SqlConnection object.
                    using (SqlConnection con = new SqlConnection()) {
            
                        // Configure the SqlConnection object's connection string.
                        con.ConnectionString = "Data Source = localhost;" + 
                            "Database = Northwind; Integrated Security=SSPI";
            
                        // Create and configure a new command that includes the
                        // FOR XML AUTO clause.
                        SqlCommand com = con.CreateCommand();
                        com.CommandType = CommandType.Text;
                        com.CommandText = "SELECT CustomerID, CompanyName" + 
                            " FROM Customers FOR XML AUTO";
            
                        // Declare an XmlReader so that it can be referenced in the 
                        // finally clause to ensure it is closed after use.
                        XmlReader reader = null;
            
                        try {
                            // Open the database connection.
                            con.Open();
            
                            // Execute the command and retrieve an XmlReader to access
                            // the results.
                            reader = com.ExecuteXmlReader();
            
                            while (reader.Read()) {
            
                                Console.Write("Element: " + reader.Name);
                                if (reader.HasAttributes) {
                                    for (int i = 0; i < reader.AttributeCount; i++) {
            
                                        reader.MoveToAttribute(i);
                                        Console.Write("  {0}: {1}",
                                            reader.Name, reader.Value);
                                    }
            
                                    // Move the XmlReader back to the element node.
                                    reader.MoveToElement();  
                                    Console.WriteLine();
                                }
                            }
                        } catch (Exception ex) {
            
                            Console.WriteLine(ex.ToString());
                        } finally {
            
                            // Ensure the reader is closed.
                            if (reader != null) reader.Close();
                        }
                    }
            
                    // Wait to continue.
                    Console.ReadLine();
                }
            }

            Some of the output from this test application is shown here:

            Element: Customers  CustomerID: ALFKI  CompanyName: Alfreds Futterkiste
            Element: Customers  CustomerID: ANTON  CompanyName: Antonio Moreno Taquería
            Element: Customers  CustomerID: GOURL  CompanyName: Gourmet Lanchonetes
            ...

            Instead of working with the XmlReader and accessing the data sequentially, you can read the XML data into a System.Xml.XmlDocument. This way, all the data is retrieved into memory, and the database connection can be closed. You can then continue to interact with the XML document. (Chapter 5 contains numerous examples of how to use the XmlDocument class.) Here's the code you would need.

            XmlDocument doc = new XmlDocument();
            
            // Create a new SqlConnection object.
            using (SqlConnection con = new SqlConnection()) {
            
                // Configure the SqlConnection object's connection string.
                con.ConnectionString = "Data Source = localhost;" + 
                    "Database = Northwind; Integrated Security=SSPI";
            
                // Create and configure a new command that includes the
                // FOR XML AUTO clause.
                SqlCommand com = con.CreateCommand();
                com.CommandType = CommandType.Text;
                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 = com.ExecuteXmlReader();
                doc.LoadXml("<results></results>");
            
                // Create an XmlNode from the next XML element read from the 
                // reader.
                XmlNode newNode = doc.ReadNode(reader);
            
                while (newNode != null) {
            
                    doc.DocumentElement.AppendChild(newNode);
                    newNode = doc.ReadNode(reader);
                }
            }
            
            //save xml file
            doc.save(myxml.xml); // Process the disconnected XmlDocument. Console.WriteLine(doc.OuterXml);

            posted on 2005-11-24 13:10 夢(mèng)在天涯 閱讀(424) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C#/.NET

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807518
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            久久精品免费一区二区| 精品久久久久久无码专区| 国产成人久久精品麻豆一区| 国产精品久久久久久一区二区三区| 色欲久久久天天天综合网精品| 久久夜色精品国产噜噜麻豆| 久久精品国产91久久麻豆自制| 秋霞久久国产精品电影院| 狠狠精品久久久无码中文字幕| 伊人久久大香线蕉综合5g| 久久久无码精品亚洲日韩按摩| 国产激情久久久久影院| 亚洲精品白浆高清久久久久久| 久久精品国产只有精品2020| 中文字幕无码久久精品青草| 久久综合国产乱子伦精品免费| 99久久www免费人成精品| 亚洲精品无码专区久久久| 久久国产成人午夜aⅴ影院| 无码人妻精品一区二区三区久久 | 亚洲AV无码久久精品狠狠爱浪潮 | 亚洲欧洲日产国码无码久久99| 亚洲成人精品久久| 777午夜精品久久av蜜臀| 色99久久久久高潮综合影院| 久久精品嫩草影院| 成人资源影音先锋久久资源网| 久久狠狠爱亚洲综合影院| 色狠狠久久综合网| 国内精品久久久久久中文字幕| 国产91久久精品一区二区| 国产aⅴ激情无码久久| 欧美精品乱码99久久蜜桃| 久久久久久av无码免费看大片| 久久精品www| 久久久久四虎国产精品| 国产成人精品久久免费动漫| 久久久精品2019免费观看| 成人综合伊人五月婷久久| 亚洲AV无码久久精品色欲| 蜜臀久久99精品久久久久久小说|