Use the ExecuteXmlReader() method to run a SELECT statement that returns XML : Database to XML : Database C# Source Code


Custom Search

C# Source Code » Database » Database to XML »

 

Use the ExecuteXmlReader() method to run a SELECT statement that returns XML








    


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

class ExecuteXmlReader
{
  public static void Main()
  {
    SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");
    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

    mySqlCommand.CommandText = "SELECT TOP 5 ID, FirstName, LastName " +
      "FROM Employee " +
      "ORDER BY ID " +
      "FOR XML AUTO";

    mySqlConnection.Open();

    XmlReader myXmlReader = mySqlCommand.ExecuteXmlReader();

    myXmlReader.Read();
    while (!myXmlReader.EOF) {
      Console.WriteLine(myXmlReader.ReadOuterXml());
    }

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

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Database
» Database to XML