Reference result set by column index in SqlDataReader : SqlDataReader : ADO.Net C# Examples


C# Examples » ADO.Net » SqlDataReader »

 

Reference result set by column index in SqlDataReader









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

class  MainClass
{
      static  void  Main(string[]  args)
      {
            string  connString  =  "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated  Security=SSPI;";

            string  sql  =  @"select  *  from  employee";

            SqlConnection  conn  =  null;
            SqlDataReader  reader  =  null;

            try
            {
                  conn  =  new  SqlConnection(connString);
                  conn.Open();

                  SqlCommand  cmd  =  new  SqlCommand(sql,  conn);
                  reader  =  cmd.ExecuteReader();

                  Console.WriteLine("Querying  database  {0}  with  query  {1}\n",  conn.Database,  cmd.CommandText);

                  while(reader.Read())  {
                        Console.WriteLine("{0}  |  {1}",  reader["FirstName"].ToString().PadLeft(10)  ,  reader[1].ToString().PadLeft(10)  );
                  }
            }catch  (Exception  e)
            {
                  Console.WriteLine("Error:  "  +  e);
            }
            finally
            {
                  reader.Close();
                  conn.Close();
            }
      }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» SqlDataReader