SqlCommand.ExecuteXmlReader() : SqlCommand « System.Data.SqlClient « C# / C Sharp by API






SqlCommand.ExecuteXmlReader()

  

using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

class ExecuteXmlReader {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

        mySqlCommand.CommandText =
          "SELECT TOP 5 ProductID, ProductName, UnitPrice " +
          "FROM Products " +
          "ORDER BY ProductID " +
          "FOR XML AUTO";

        mySqlConnection.Open();
        XmlReader myXmlReader = mySqlCommand.ExecuteXmlReader();
        myXmlReader.Read();
        while (!myXmlReader.EOF) {
            Console.WriteLine(myXmlReader.ReadOuterXml());
        }

        myXmlReader.Close();
        mySqlConnection.Close();
    }
}

   
    
  








Related examples in the same category

1.SqlCommand.BeginExecuteReader
2.SqlCommand.CommandText
3.SqlCommand.CommandType
4.SqlCommand.EndExecuteReader
5.SqlCommand.ExecuteNonQuery()
6.SqlCommand.ExecuteScalar()
7.SqlCommand.Parameters
8.SqlCommand.Parameters.Add