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

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

搜索

  •  

積分與排名

  • 積分 - 1818546
  • 排名 - 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| 亚洲精品专区| 国产精品大全| 欧美诱惑福利视频| 久久精品视频在线| 亚洲精品日韩精品| 一区二区不卡在线视频 午夜欧美不卡在| 欧美日韩少妇| 久久成人免费电影| 猫咪成人在线观看| 亚洲视频中文| 久久aⅴ国产欧美74aaa| 最新日韩在线视频| 99一区二区| 国产日韩一区欧美| 免费不卡在线观看av| 欧美区国产区| 久久久精品免费视频| 欧美国产日韩xxxxx| 午夜久久99| 美女精品自拍一二三四| 亚洲在线中文字幕| 久久九九有精品国产23| 中文日韩在线视频| 久久精品国产一区二区三| av不卡在线观看| 欧美一区二区在线免费观看| 亚洲日本免费电影| 午夜精品久久久久久久久| 亚洲黄色成人久久久| 亚洲制服av| 亚洲精品国产精品国自产观看浪潮 | 欧美激情小视频| 校园春色综合网| 牛牛精品成人免费视频| 亚洲欧美久久久| 久久免费高清| 亚洲免费在线播放| 每日更新成人在线视频| 欧美亚洲专区| 一区二区三区四区五区视频| 国外成人在线视频| 一区二区三区免费网站| 亚洲丁香婷深爱综合| 亚洲自拍啪啪| 欧美 亚欧 日韩视频在线| 国产精品草草| 欧美高清视频一区| 国产日韩在线看| av成人免费在线观看| 在线精品视频在线观看高清 | 亚洲美女视频在线观看| 午夜国产精品视频| 一本久道久久综合婷婷鲸鱼| 久久久久99| 午夜亚洲伦理| 欧美日韩视频在线一区二区 | 亚洲国产视频直播| 国产精品福利在线观看| 欧美大片在线观看一区| 欧美视频在线观看免费| 亚洲高清激情| 伊人成人网在线看| 亚洲欧美综合| 亚洲一区二区三区精品动漫| 你懂的成人av| 免费永久网站黄欧美| 国产婷婷色综合av蜜臀av| 一区二区三区 在线观看视频| 亚洲国产欧美日韩另类综合| 久久成人人人人精品欧| 国产视频一区在线观看一区免费| 亚洲美女精品一区| 亚洲日本va在线观看| 久久美女艺术照精彩视频福利播放| 欧美一区二区视频在线观看| 欧美日韩三级电影在线| 亚洲精品国产系列| 亚洲精品一区二区三区婷婷月 | 免费视频最近日韩| 久久久久综合| 国产欧美一区二区三区另类精品| 中文成人激情娱乐网| 日韩手机在线导航| 欧美黄在线观看| 亚洲国产福利在线| 亚洲黄一区二区三区| 久久全球大尺度高清视频| 久久久久久亚洲精品中文字幕| 国产伦精品一区| 亚洲一区欧美一区| 先锋影音国产一区| 国产精品亚洲综合| 亚洲午夜精品在线| 日韩亚洲欧美成人| 欧美劲爆第一页| 亚洲欧洲日韩在线| 99亚洲伊人久久精品影院红桃| 欧美成人免费在线观看| 亚洲国产精品成人综合色在线婷婷| 亚洲电影免费观看高清| 美国成人毛片| 亚洲国产欧美久久| 美日韩精品免费观看视频| 亚洲乱码国产乱码精品精98午夜 | 影音国产精品| 久久久激情视频| 免费看av成人| 亚洲欧洲一区二区三区久久| 男女视频一区二区| 最新高清无码专区| 在线综合欧美| 国产精品欧美日韩| 亚洲综合视频一区| 久久国产欧美精品| 好看不卡的中文字幕| 久久久激情视频| 亚洲高清久久| 亚洲天堂av图片| 国产精品欧美日韩一区| 午夜日本精品| 蜜臀a∨国产成人精品| 亚洲国产精品尤物yw在线观看 | 国产精品av久久久久久麻豆网| 一区二区三区欧美激情| 亚洲欧美日韩精品久久亚洲区 | 国产精品中文在线| 欧美与黑人午夜性猛交久久久| 久久中文精品| 亚洲精品在线视频| 欧美性大战久久久久久久蜜臀| 亚洲欧美成人一区二区三区| 欧美一区二区三区在线播放| 合欧美一区二区三区| 免费在线观看成人av| 日韩亚洲精品电影| 久久高清国产| 狠狠色狠狠色综合日日五| 久久精品五月婷婷| 亚洲黄网站在线观看| 亚洲欧美日韩一区在线| 好吊一区二区三区| 欧美精品免费观看二区| 亚洲综合久久久久| 欧美成人第一页| 亚洲一区二区三区精品动漫| 国产欧美一区二区精品性 | 国产无一区二区| 久久精品二区三区| 最新亚洲电影| 欧美自拍偷拍| 亚洲精品欧美一区二区三区| 国产精品美女在线| 美女免费视频一区| 99这里只有精品| 久久亚裔精品欧美| 这里是久久伊人| 合欧美一区二区三区| 欧美日韩一区二区三区高清| 欧美中文在线视频| 欧美日韩一级黄| 久久精品国产第一区二区三区| 欧美成人精品激情在线观看| 亚洲淫片在线视频| 亚洲国产精品尤物yw在线观看| 国产精品素人视频| 欧美不卡视频一区发布| 亚洲免费在线| 亚洲人成亚洲人成在线观看| 欧美一区二区三区在线免费观看| 亚洲国产日韩欧美在线99| 国产精品推荐精品| 欧美电影在线观看| 久久不射网站| 99人久久精品视频最新地址| 久久午夜精品一区二区| 亚洲天天影视| 91久久国产综合久久蜜月精品| 国产精品一区二区你懂的| 欧美激情一区二区久久久| 久久福利影视| 亚洲调教视频在线观看| 欧美国产国产综合| 午夜视频在线观看一区二区| 亚洲美女性视频| 国产综合久久| 国产精品萝li| 欧美精品一区二区精品网| 久久久精彩视频| 午夜精品美女自拍福到在线 | 你懂的视频一区二区| 亚洲欧洲av一区二区三区久久| 亚洲剧情一区二区| 在线观看日韩专区| 国产一区999| 国产精品一页| 欧美午夜性色大片在线观看| 欧美国产精品一区|