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

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 夢在天涯 閱讀(430) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1811723
  • 排名 - 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>
              亚洲综合日韩在线| 国产综合精品| 性伦欧美刺激片在线观看| 亚洲黄一区二区| 欧美一区二区三区另类 | 亚洲人成亚洲人成在线观看图片 | 欧美极品在线播放| 欧美777四色影视在线| 久久久综合网| 欧美人妖另类| 欧美视频精品在线| 欧美一区二区三区另类 | 亚洲精品社区| 午夜亚洲福利| 亚洲午夜羞羞片| 国产精品成人午夜| 午夜精品久久久久影视| 亚洲精品视频在线观看网站| 久久精品二区| 久久久亚洲人| 亚洲你懂的在线视频| 国产精品h在线观看| 欧美国产日本| 日韩午夜激情电影| 榴莲视频成人在线观看| 欧美成人精品在线播放| 黄色精品免费| 欧美精品www| 欧美大片在线观看一区| 一本色道久久综合一区| 亚洲一区二区免费在线| 欧美一区二区在线播放| 国产欧美日本| 国产精品久久久一区麻豆最新章节| 欧美xx视频| 麻豆九一精品爱看视频在线观看免费| 中文欧美日韩| 亚洲欧洲日产国产综合网| 亚洲人成网站在线观看播放| 国产欧美日韩一级| 欧美日韩国产丝袜另类| 久久免费视频网| 亚洲精品女人| 久久夜色精品一区| 国产精品99久久久久久有的能看| 亚洲福利国产| 国产日韩欧美另类| 国产精品任我爽爆在线播放| 亚洲国产一区视频| 欧美精品一区二区三区一线天视频| 久热精品视频在线| 欧美日韩亚洲一区二区三区四区| 国产精品成人免费视频| 黄页网站一区| 国内精品视频在线播放| 一区二区三区日韩| 精品二区视频| 日韩午夜在线电影| 亚洲综合日韩中文字幕v在线| 亚洲小说欧美另类社区| 欧美精品福利| 欧美精品久久一区| 国产三级精品三级| 国产日韩精品一区| 一区二区视频免费在线观看| 亚洲激情成人在线| 午夜日韩在线观看| 亚洲二区在线观看| 亚洲欧美日韩成人| 欧美成人精品高清在线播放| 欧美日韩国产在线播放网站| 欧美freesex8一10精品| 一区二区日韩精品| 免费观看日韩av| 亚洲精品网址在线观看| 亚洲欧美在线看| 久久午夜精品| 国产午夜精品理论片a级大结局| 亚洲人成网站精品片在线观看| 午夜精品亚洲| 亚洲精品网址在线观看| 韩国av一区二区三区| 性欧美精品高清| 美女久久网站| 一区二区三区毛片| 老司机67194精品线观看| 国产欧美日韩亚洲精品| 亚洲欧美国产不卡| 亚洲性视频h| 国产精品激情电影| 亚洲一区二区3| 亚洲天堂成人| 国产精品一区三区| 欧美在线3区| 欧美一区网站| 亚洲国产欧美不卡在线观看| 麻豆av福利av久久av| 香港久久久电影| 韩曰欧美视频免费观看| 中文一区二区在线观看| 久久精品人人爽| 久久久av毛片精品| 日韩视频在线永久播放| 一本色道久久综合狠狠躁篇怎么玩 | 嫩草国产精品入口| 欧美在线电影| 亚洲国产精品久久久久婷婷884| 欧美二区视频| 欧美先锋影音| 欧美一级淫片aaaaaaa视频| 亚洲欧洲一二三| 麻豆精品国产91久久久久久| 久久久999精品| 中日韩视频在线观看| 久久精品亚洲| 亚洲四色影视在线观看| 久久久欧美精品| 亚洲欧美日韩一区二区三区在线| 欧美在线日韩在线| 一区二区欧美在线| 久久九九国产精品| 午夜精品在线| 欧美另类视频| 久久久亚洲国产天美传媒修理工| 久久久久久久综合色一本| 国产精品久久久久久一区二区三区| 久久久久久久久岛国免费| 欧美经典一区二区三区| 美日韩在线观看| 欧美在线视频观看免费网站| 亚洲私人影吧| 欧美精品久久久久久久免费观看| 久久九九国产| 亚洲大片精品永久免费| 亚洲看片网站| 亚洲第一福利在线观看| 欧美大学生性色视频| 美女精品在线| 性欧美办公室18xxxxhd| 欧美日韩中文字幕日韩欧美| 久久久久久亚洲精品杨幂换脸 | 午夜影视日本亚洲欧洲精品| 亚洲欧洲一区二区三区久久| 久久国产直播| 一区二区三区欧美视频| 欧美日韩日本国产亚洲在线 | 久久九九国产精品| 另类av一区二区| 久久riav二区三区| 欧美激情一区| 亚洲欧洲日本在线| 欧美日韩国产色综合一二三四 | 国产一区二区三区在线观看精品| 99国产精品久久久久老师| 午夜在线视频一区二区区别| 国产精品日本精品| 久久资源在线| 亚洲视频在线看| 久久综合综合久久综合| 中文日韩电影网站| 国产精品成人在线| 久久久久久久999精品视频| 亚洲国产精品一区制服丝袜| 亚洲综合国产激情另类一区| 一区二区三区在线视频免费观看| 美女视频黄a大片欧美| 亚洲精品一二三| 久久精品水蜜桃av综合天堂| 国产日韩欧美制服另类| 久久国产精品免费一区| 免费观看在线综合| 亚洲午夜小视频| 日韩视频中午一区| 亚洲人成久久| 一区二区三区日韩在线观看| 亚洲黄色有码视频| 亚洲韩国日本中文字幕| 亚洲精品1234| 亚洲视频欧美视频| 欧美一区二区三区久久精品| 日韩视频在线观看国产| 麻豆久久婷婷| 亚洲一区二区三区四区五区黄| 最新国产の精品合集bt伙计| 美女露胸一区二区三区| 亚洲欧洲偷拍精品| 欧美91视频| 美女诱惑黄网站一区| 蜜桃av综合| 欧美高清视频免费观看| 久久精品国产视频| 久久天天躁狠狠躁夜夜爽蜜月 | 一本大道久久a久久综合婷婷| 亚洲国产小视频| 亚洲人成欧美中文字幕| 亚洲人成人一区二区在线观看| 亚洲人成在线播放网站岛国| 一区二区国产日产| 久久久久久久久久久久久久一区| 久久久久成人精品免费播放动漫|