青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

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 夢在天涯 閱讀(433) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1816885
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              老司机午夜免费精品视频| 久久免费观看视频| 亚洲美洲欧洲综合国产一区| 亚洲精品视频在线观看免费| 亚洲一二三四区| 欧美专区日韩专区| 你懂的视频欧美| 在线视频精品一| 久久疯狂做爰流白浆xx| 欧美精品九九| 国产视频自拍一区| 亚洲久色影视| 日韩视频一区二区三区在线播放免费观看 | 欧美日本视频在线| 国产日韩欧美在线一区| 久久久久成人精品| 夜夜嗨av色综合久久久综合网| 性伦欧美刺激片在线观看| 欧美国产亚洲另类动漫| 国产一区二区三区在线观看精品| 一区二区三区鲁丝不卡| 看片网站欧美日韩| 亚洲视频中文| 欧美日韩午夜在线| 亚洲大胆人体视频| 欧美在线观看你懂的| 亚洲精品小视频在线观看| 久久午夜视频| 国产曰批免费观看久久久| 亚洲免费视频在线观看| 亚洲成色777777女色窝| 日韩视频在线观看国产| 久久久久国产一区二区三区四区| 久久久久免费观看| 精品动漫3d一区二区三区| 午夜在线一区| 亚洲一区在线直播| 欧美人交a欧美精品| 校园激情久久| 欧美1区2区3区| 亚洲电影专区| 麻豆久久久9性大片| 欧美日韩伦理在线| 麻豆精品一区二区综合av| 欧美日韩精品综合| 亚洲永久在线| 亚洲欧美日本另类| 国产一区二区中文字幕免费看| 亚洲国产三级在线| 欧美国产视频日韩| 玖玖在线精品| 国产日韩欧美精品综合| 99综合电影在线视频| 在线视频成人| 久久国产精品99国产精| 欧美一级免费视频| 欧美深夜福利| 欧美亚洲免费电影| 欧美日韩视频在线观看一区二区三区 | 麻豆精品视频在线| 亚洲国产专区校园欧美| 亚洲精品乱码久久久久久蜜桃麻豆| 欧美区一区二| 亚洲激情视频在线观看| 欧美日韩一区在线| 亚洲精品少妇| 99视频精品| 香港成人在线视频| 欧美一级淫片aaaaaaa视频| 欧美日韩伦理在线| 日韩小视频在线观看| 9久re热视频在线精品| 欧美国产精品久久| 亚洲国产精品久久91精品| 尤物九九久久国产精品的特点| 欧美激情一区二区三区在线| 欧美深夜福利| 亚洲亚洲精品三区日韩精品在线视频 | 欧美专区在线观看| 夜夜夜精品看看| 欧美日本精品一区二区三区| 亚洲精品久久久蜜桃| 日韩一级网站| 国产精品萝li| 欧美超级免费视 在线| 亚洲国产精品成人综合色在线婷婷| 久久久久久高潮国产精品视| 免费亚洲一区| 国产日韩亚洲欧美精品| 欧美在线亚洲综合一区| 久久综合狠狠综合久久综青草| 欧美精品v日韩精品v国产精品| 亚洲精品一区二区三区婷婷月 | 最新国产成人在线观看| 亚洲一区二区三区在线| 久久av在线看| 亚洲国产成人91精品| 欧美国产激情| 亚洲综合精品一区二区| 亚洲免费av观看| 国产精品视频福利| 亚洲麻豆av| 欧美资源在线| 亚洲精品乱码久久久久久日本蜜臀 | 一本色道久久88综合亚洲精品ⅰ| 永久久久久久| 欧美日韩专区在线| 久久精品30| 日韩亚洲成人av在线| 久久久91精品国产一区二区精品| 亚洲日韩中文字幕在线播放| 国产精品久久久久毛片软件| 久久久www成人免费毛片麻豆| 久久精品国产2020观看福利| 国产精品豆花视频| 亚洲午夜视频在线观看| 欧美mv日韩mv亚洲| 亚洲欧美激情一区二区| 精品99一区二区| 国产精品二区二区三区| 另类春色校园亚洲| 亚洲欧美一区二区视频| 欧美激情无毛| 久久伊人亚洲| 亚洲欧美国产77777| 亚洲精品乱码久久久久久黑人| 国产日本欧美视频| 欧美在线视频一区二区| 亚洲色诱最新| 亚洲精品乱码久久久久久蜜桃麻豆| 久久九九精品99国产精品| 亚洲一区久久久| 亚洲精品国产精品国自产在线| 国产日韩精品在线| 国产精品男女猛烈高潮激情| 欧美精品一区二| 欧美~级网站不卡| 久久久久国色av免费看影院| 欧美一区二区三区免费观看| 美女日韩在线中文字幕| 欧美专区18| 久久国产66| 欧美自拍偷拍午夜视频| 午夜亚洲性色视频| 国产综合久久| 国产视频欧美| 国产伦精品一区二区三区四区免费| 久久精品国产欧美亚洲人人爽| 午夜精品久久久久久久久| 亚洲欧美国内爽妇网| 亚洲免费在线精品一区| 亚洲视频日本| 亚洲综合欧美日韩| 亚洲男人影院| 欧美一级二级三级蜜桃| 午夜精品亚洲| 久久精品国产精品| 久久久免费av| 欧美韩日视频| 欧美日韩一区二区三区高清| 欧美色欧美亚洲另类二区| 欧美网站大全在线观看| 久久综合综合久久综合| 久久亚洲二区| 欧美精品一区二| 国产精品美女www爽爽爽| 国产精品美女久久久久久2018 | 欧美激情亚洲视频| 欧美日韩国产成人| 国产精品欧美日韩| 国产一区二区三区在线观看精品 | 亚洲高清一区二区三区| 99re热精品| 先锋资源久久| 免费久久99精品国产自| 亚洲巨乳在线| 亚洲欧美视频在线观看| 老司机aⅴ在线精品导航| 欧美日韩国产bt| 国产视频一区在线观看| 亚洲精品看片| 小嫩嫩精品导航| 欧美电影电视剧在线观看| 久久久久久久久久看片| 欧美激情一区二区三区蜜桃视频| av成人毛片| 久久久精品一区| 国产精品初高中精品久久| 雨宫琴音一区二区在线| 亚洲影院在线| 欧美第一黄色网| 亚洲影院免费观看| 欧美成人免费在线| 欧美成人一品| 国产日韩专区在线| 日韩视频在线永久播放| 久久精品国产v日韩v亚洲| 最新中文字幕一区二区三区| 久久www成人_看片免费不卡| 欧美日韩在线看|