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

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

搜索

  •  

積分與排名

  • 積分 - 1817646
  • 排名 - 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>
              在线日韩日本国产亚洲| 亚洲美女电影在线| 欧美中文日韩| 亚洲欧美久久久| 国产精品日日摸夜夜摸av| 亚洲综合国产精品| 亚洲一区二区在线免费观看视频 | 蜜臀av国产精品久久久久| 久久国产福利国产秒拍| 伊人精品成人久久综合软件| 免费影视亚洲| 欧美日本韩国| 久久av在线看| 蜜臀av性久久久久蜜臀aⅴ| 日韩视频不卡中文| 亚洲一区二区三区精品在线观看| 国产日韩欧美a| 欧美国产在线视频| 欧美视频在线看| 久久久精品网| 欧美剧在线免费观看网站| 午夜亚洲性色福利视频| 久久久久久一区二区| 在线亚洲电影| 久久视频一区| 亚洲永久精品大片| 美女视频黄免费的久久| 亚洲性图久久| 麻豆久久精品| 欧美在线观看网址综合| 欧美本精品男人aⅴ天堂| 亚洲免费一级电影| 欧美gay视频| 欧美一区二区高清| 欧美精品一区二区三区在线看午夜 | 亚洲毛片在线| 欧美在线视屏| 亚洲一区二区在线视频| 美女视频黄a大片欧美| 欧美伊人久久| 欧美日本高清| 亚洲高清资源| 国产在线精品成人一区二区三区| 亚洲激情自拍| 亚洲国产精品传媒在线观看| 亚洲欧美不卡| 午夜精品在线视频| 欧美日韩大片| 亚洲国产综合在线| 在线观看三级视频欧美| 欧美资源在线观看| 欧美中文在线观看| 国产精品入口66mio| 亚洲免费观看| 亚洲美女淫视频| 你懂的网址国产 欧美| 乱中年女人伦av一区二区| 国产日韩欧美精品在线| 亚洲一级一区| 午夜视频精品| 国产精品丝袜久久久久久app| 亚洲精品欧美在线| 亚洲精品极品| 玖玖综合伊人| 欧美激情一区二区三区蜜桃视频| 极品少妇一区二区| 久久国产欧美精品| 老司机久久99久久精品播放免费| 国产一区二区三区免费观看| 欧美一区二区三区免费大片| 久久精品国产亚洲一区二区三区 | 欧美午夜免费电影| 中文精品视频| 久久国产免费| 黄色工厂这里只有精品| 久久久噜噜噜久久| 亚洲国产精品电影在线观看| 99亚洲一区二区| 欧美午夜美女看片| 午夜精品美女自拍福到在线| 久久久久久久综合色一本| 激情欧美一区二区三区| 久久久久在线| 亚洲精品日韩一| 香港成人在线视频| 好看的av在线不卡观看| 美女精品网站| 99精品国产99久久久久久福利| 亚洲欧美国产精品va在线观看 | 免费看的黄色欧美网站| 亚洲精品极品| 欧美午夜片在线观看| 亚洲午夜激情网页| 久久美女性网| 亚洲国产日韩在线| 欧美一区二区免费视频| 久久国产精品久久w女人spa| 一区二区欧美精品| 亚洲韩国青草视频| 亚洲成人在线视频播放| 国产精品青草久久| 国产精品高清在线| 欧美视频成人| 欧美色视频一区| 欧美日韩高清区| 欧美日韩国产色综合一二三四| 欧美激情一区二区三区四区| 美女精品自拍一二三四| 蜜桃av一区二区| 免费一级欧美在线大片| 牛牛影视久久网| 欧美成人免费网站| 欧美久久一级| 欧美日韩精品在线视频| 欧美日韩亚洲系列| 国产精品大片wwwwww| 国产精品一区一区三区| 国产中文一区| 亚洲国产精品成人久久综合一区| 亚洲丰满在线| 99亚洲视频| 亚洲欧美日韩国产成人精品影院 | 狠狠色综合日日| 在线国产精品一区| 亚洲韩国一区二区三区| aa级大片欧美三级| 亚洲一区在线视频| 久久精品一级爱片| 欧美大片在线看免费观看| 91久久精品美女高潮| 99re6这里只有精品| 亚洲欧美国产日韩中文字幕 | 亚洲制服av| 久久久精品一品道一区| 欧美+亚洲+精品+三区| 欧美视频国产精品| 国产在线不卡| 在线视频亚洲| 久久免费视频一区| 亚洲日本成人网| 亚洲欧美精品在线观看| 牛人盗摄一区二区三区视频| 国产精品www994| 国产午夜精品理论片a级大结局| 亚洲电影av| 亚洲欧美日本国产专区一区| 麻豆亚洲精品| 中文国产成人精品| 久久精品亚洲精品国产欧美kt∨| 欧美日韩精品| 亚洲第一福利在线观看| 亚洲免费一区二区| 亚洲第一主播视频| 亚洲欧美清纯在线制服| 欧美激情中文字幕一区二区| 国产日韩欧美中文| 亚洲天堂av综合网| 欧美成人国产一区二区| 亚洲欧美日韩国产成人精品影院| 欧美成人69av| 国产综合色在线视频区| 亚洲一区二区三区国产| 亚洲国产一区二区三区青草影视| 亚洲欧美日韩国产综合| 欧美日韩a区| 亚洲精品久久久久久一区二区| 久久福利毛片| 亚洲永久字幕| 欧美日韩午夜| 夜夜狂射影院欧美极品| 欧美刺激性大交免费视频| 久久er精品视频| 国产精品男gay被猛男狂揉视频| 日韩一级黄色av| 欧美激情性爽国产精品17p| 欧美一区亚洲一区| 国产日本欧美一区二区| 亚洲专区欧美专区| 日韩特黄影片| 欧美日韩一级视频| 日韩图片一区| 亚洲精品欧洲精品| 国产精品毛片高清在线完整版| 1000部国产精品成人观看 | 国产精品资源在线观看| 一本色道久久综合一区| 欧美大胆a视频| 久热国产精品| 在线成人免费视频| 久久综合伊人77777尤物| 欧美专区在线观看一区| 黄色国产精品| 欧美成人免费小视频| 久久午夜国产精品| 亚洲黄网站在线观看| 亚洲国产欧美日韩精品| 欧美激情a∨在线视频播放| 一区二区三区精品视频在线观看| 亚洲激情专区| 国产精品盗摄一区二区三区|