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

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

搜索

  •  

積分與排名

  • 積分 - 1812984
  • 排名 - 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>
              欧美一区二区视频在线| 免费成人高清视频| 久久久久久亚洲精品中文字幕| 国产日本欧美视频| 国产日韩在线播放| 尤物yw午夜国产精品视频明星| 欧美xx视频| 欧美精品成人| 国产精品夜夜夜一区二区三区尤| 久久久一二三| 欧美~级网站不卡| 欧美视频精品一区| 国精品一区二区| 国产精品久久久久久久一区探花| 久久精品欧美| 一区二区三区免费网站| 亚洲欧美在线x视频| 亚洲欧洲日本mm| 在线视频欧美精品| 久久在线观看视频| 国产精品欧美一区喷水| 亚洲国产裸拍裸体视频在线观看乱了中文 | 久久激情视频久久| 男女精品网站| 国产欧美日韩高清| 亚洲蜜桃精久久久久久久| 欧美有码在线观看视频| 亚洲欧美另类在线| 免费成人在线观看视频| 亚洲综合成人在线| 久久最新视频| 久久国产婷婷国产香蕉| 亚洲自拍偷拍福利| 欧美成人黑人xx视频免费观看| 久久亚洲国产精品一区二区 | 一本色道婷婷久久欧美| 欧美一区二区三区视频在线| 欧美激情第六页| 亚洲高清不卡一区| 久久久人成影片一区二区三区观看 | 欧美α欧美αv大片| 国产午夜精品久久久久久久| 亚洲一区欧美激情| 亚洲精品你懂的| 欧美激情成人在线视频| 亚洲电影下载| 免费观看亚洲视频大全| 新狼窝色av性久久久久久| 欧美视频网址| 亚洲性人人天天夜夜摸| 最近中文字幕mv在线一区二区三区四区| 欧美gay视频激情| 午夜影院日韩| 国产日韩精品在线观看| 欧美在线视频在线播放完整版免费观看| 午夜在线不卡| 亚洲一区二区三区四区视频| 欧美性猛交xxxx免费看久久久| 国产欧美视频一区二区三区| 亚洲欧美成aⅴ人在线观看| 一区二区激情小说| 欧美日韩国产在线| 亚洲免费婷婷| 亚洲欧美在线视频观看| 国产欧美日韩不卡| 91久久中文| 亚洲大胆女人| 欧美精品成人| 国产综合网站| 欧美18av| 欧美黑人在线观看| 亚洲一区二区日本| 亚洲综合电影一区二区三区| 国产欧美综合在线| 免费成人av在线| 欧美精品在线免费| 亚洲免费在线观看视频| 欧美一区二区视频在线观看2020| 欧美日本一区二区视频在线观看| 国产色婷婷国产综合在线理论片a| 最新亚洲一区| 亚洲精品免费在线观看| 国产精品嫩草久久久久| 另类专区欧美制服同性| 亚洲午夜久久久久久久久电影网| 久久免费精品日本久久中文字幕| 亚洲狼人精品一区二区三区| 欧美日韩在线高清| 久久精品理论片| 免费成人高清| 欧美在线视频网站| 男人天堂欧美日韩| 性欧美video另类hd性玩具| 另类欧美日韩国产在线| 亚洲尤物视频网| 日韩一区二区久久| 国产一区日韩一区| 夜夜狂射影院欧美极品| 欧美人在线观看| 久久精品一二三| 欧美另类亚洲| 久久综合图片| 国产精品一卡二| 日韩午夜av电影| 亚洲国产综合视频在线观看| 亚洲私人影院在线观看| 欧美日韩中文字幕精品| 亚洲精品一区久久久久久| 亚洲综合色自拍一区| 亚洲国产精品久久久| 午夜久久黄色| 亚洲自拍啪啪| 一本久久a久久精品亚洲| 在线电影一区| 久久久精品动漫| 亚洲激情综合| 欧美专区在线观看| 欧美在线不卡视频| 国产精品成人国产乱一区| 亚洲国产精品一区在线观看不卡| 欧美黑人在线观看| 久久视频国产精品免费视频在线| 久久久久久久999| 在线看日韩欧美| 欧美亚洲一级| 久久精品综合一区| 国产日韩在线一区| 亚洲欧美精品| 午夜一区在线| 国产精品尤物| 免费在线观看一区二区| 国产日韩精品入口| 欧美在线观看视频一区二区| 欧美在线视频免费| 国产一区在线播放| 欧美在线视频一区| 久久久一区二区三区| 国产综合自拍| 亚洲最新在线| 亚洲图片在线观看| 国产精品国产三级国产aⅴ入口| 久久久福利视频| 国产综合欧美| 乱中年女人伦av一区二区| 欧美成人在线网站| 亚洲精品一区久久久久久| 欧美激情精品久久久久久免费印度| 亚洲在线视频| 国产精品丝袜久久久久久app| 久久综合免费视频影院| 影音先锋久久资源网| 米奇777超碰欧美日韩亚洲| 一区二区三区国产精华| 欧美日韩一区二区三区免费看| 久久精品亚洲一区二区| 国产精品久久久久毛片软件| 欧美大秀在线观看| 亚洲伦理精品| 国产精品一区二区三区免费观看| 欧美成人中文字幕在线| 日韩网站在线| 久久综合久久综合久久综合| 欧美激情亚洲另类| 亚洲午夜在线| 在线高清一区| 国产精品久久久爽爽爽麻豆色哟哟 | 久久久久久午夜| 亚洲激情影视| 久久精品国产91精品亚洲| 亚洲国产成人久久综合| 欧美视频在线免费看| 久久国产主播精品| 亚洲精品久久久久久下一站| 久久精品一区二区三区不卡牛牛| 国产精品黄视频| 久久精品男女| 在线视频一区二区| 欧美激情一区二区三区| 欧美一区二区三区在线视频| 91久久在线观看| 国产一区二区三区免费不卡| 欧美喷水视频| 久久久国产精彩视频美女艺术照福利| 欧美一区二区三区免费在线看| 欧美片在线观看| 性xx色xx综合久久久xx| 亚洲精品欧美日韩专区| 一区二区三区蜜桃网| 国产人成一区二区三区影院| 欧美伦理影院| 免费视频一区| 久久狠狠久久综合桃花| 亚洲一卡二卡三卡四卡五卡| 亚洲人人精品| 欧美激情自拍| 欧美成人精品不卡视频在线观看| 亚洲国产精品久久久久| 国产女人18毛片水18精品| 亚洲欧美日韩一区二区| 日韩视频在线观看一区二区|