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

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>
              亚洲一区在线播放| 亚洲永久免费精品| 免费国产一区二区| 免费美女久久99| 91久久久久久国产精品| 亚洲精品黄色| 国产精品video| 午夜精品一区二区在线观看 | 午夜精品福利一区二区三区av | 另类春色校园亚洲| 蜜桃久久精品乱码一区二区| 亚洲精品久久久久久久久久久久 | 亚洲免费视频成人| 欧美在线观看视频一区二区三区| 在线精品福利| 亚洲乱码久久| 国产亚洲成年网址在线观看| 蜜臀av性久久久久蜜臀aⅴ| 欧美暴力喷水在线| 香蕉视频成人在线观看| 久久香蕉国产线看观看网| 亚洲乱码国产乱码精品精| 亚洲综合精品四区| 亚洲大片av| 亚洲图中文字幕| 亚洲国产91| 国内偷自视频区视频综合| 免费在线亚洲| 国产精品亚发布| 最近看过的日韩成人| 国产精品永久免费| 亚洲精品久久久久久下一站| 国产亚洲一区在线播放| 99在线精品视频| 亚洲第一中文字幕在线观看| 一区二区国产日产| 最新69国产成人精品视频免费| 一区二区三区精品在线| 一区免费观看| 亚洲男人的天堂在线| 日韩视频不卡| 蜜臀av性久久久久蜜臀aⅴ四虎| 亚洲欧美视频在线观看视频| 欧美不卡一区| 另类天堂av| 国产精品永久免费| 一区二区三区高清不卡| 亚洲乱码国产乱码精品精天堂 | 一区二区三区久久久| 最新亚洲视频| 久久先锋资源| 久久在线免费观看视频| 国产日韩在线看| 亚洲一级特黄| 亚洲免费在线视频一区 二区| 欧美激情精品久久久久久免费印度| 久久久久成人精品| 国产小视频国产精品| 亚洲午夜小视频| 亚洲伊人久久综合| 欧美色123| 日韩亚洲精品在线| 中文高清一区| 欧美日韩视频专区在线播放| 亚洲激情成人| 99亚洲一区二区| 欧美日韩精品中文字幕| 91久久综合亚洲鲁鲁五月天| 亚洲三级电影在线观看| 免费在线观看精品| 亚洲国产免费| 99精品视频免费观看| 欧美日韩免费看| 一本一本a久久| 亚洲欧美另类在线| 国产精品网站在线| 欧美在线免费看| 蜜桃久久精品乱码一区二区| 亚洲国产欧美国产综合一区| 欧美a级在线| 99热在线精品观看| 欧美在线观看视频| 黄色影院成人| 欧美国产视频日韩| 在线综合亚洲| 久久久国产成人精品| 一区久久精品| 欧美日本免费一区二区三区| 亚洲色在线视频| 久久亚洲精品伦理| 亚洲区欧美区| 国产精品成人在线| 欧美主播一区二区三区| 亚洲激情一区| 欧美一区二区三区日韩视频| 一区视频在线看| 欧美日韩在线综合| 久久精品99国产精品| 亚洲国产成人porn| 午夜亚洲福利| 亚洲欧洲精品一区二区| 国产精品久久网| 老司机成人在线视频| 一区二区日韩伦理片| 麻豆国产精品777777在线 | 国内精品视频在线观看| 欧美精品www在线观看| 欧美亚洲自偷自偷| 日韩亚洲成人av在线| 久久亚洲综合网| 亚洲一区欧美二区| 亚洲精品视频二区| 国产日韩精品一区二区三区在线| 欧美大片18| 欧美在线观看一二区| 一区二区三区欧美在线| 欧美成人69av| 久久国产黑丝| 亚洲视频二区| 亚洲精选视频免费看| 国产原创一区二区| 国产精品福利在线观看| 欧美国产日本高清在线| 欧美一区二区三区在线| 宅男精品视频| 亚洲精品美女| 欧美国产先锋| 久久躁狠狠躁夜夜爽| 久久精品99国产精品酒店日本| 亚洲无线视频| 亚洲最新在线| 日韩视频免费在线观看| 亚洲成人自拍视频| 在线播放视频一区| 国模私拍视频一区| 国产日韩欧美成人| 国产日韩亚洲欧美精品| 国产女人aaa级久久久级| 欧美视频中文一区二区三区在线观看| 久久亚洲图片| 久久一区二区三区四区五区| 久久久之久亚州精品露出| 久久精品国产亚洲a| 久久久精品2019中文字幕神马| 欧美一区二区免费| 久久久久久免费| 久久av资源网站| 久久久夜精品| 免费在线播放第一区高清av| 蜜桃伊人久久| 欧美黄在线观看| 欧美日韩和欧美的一区二区| 欧美日韩免费观看一区三区| 欧美三级乱码| 国产美女在线精品免费观看| 国产一二精品视频| 在线观看欧美| 亚洲美女精品久久| 亚洲一区二区三区涩| 先锋a资源在线看亚洲| 久久精品最新地址| 嫩草国产精品入口| 最近看过的日韩成人| 亚洲午夜视频在线| 久久久精品国产一区二区三区| 久久婷婷国产综合尤物精品| 欧美激情在线观看| 国产精品日韩精品| 亚洲电影中文字幕| 在线视频免费在线观看一区二区| 亚洲一区不卡| 久久久久久91香蕉国产| 欧美成人国产va精品日本一级| 亚洲狠狠婷婷| 欧美亚洲免费| 欧美激情小视频| 国产欧美日韩不卡| 最新国产成人av网站网址麻豆 | 国产日本欧洲亚洲| 亚洲欧洲精品一区| 欧美一区激情| 亚洲国产影院| 亚洲一区二区在线| 免费在线视频一区| 国产欧美日韩一区二区三区| 亚洲国产免费| 欧美在线综合视频| 日韩一级在线| 久久久人人人| 国产精品嫩草99a| 亚洲精品欧美日韩专区| 久久国产精品久久久| 亚洲国产一区二区三区a毛片| 亚洲免费伊人电影在线观看av| 嫩草国产精品入口| 国产一区二区无遮挡| 亚洲一区二区三区免费视频| 亚洲国产精选| 久久综合伊人| 国产一区二区视频在线观看|