Use indexer to read data : SqlDataReader : ADO.Net C# Examples


C# Examples » ADO.Net » SqlDataReader »

 

Use indexer to read data









    
using  System;
using  System.Data.SqlClient;

class  FirstExample  {
        public  static  void  Main()  {
                try  {
                        SqlConnection  mySqlConnection  =  new  SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");

                        SqlCommand  mySqlCommand  =  mySqlConnection.CreateCommand();

                        mySqlCommand.CommandText  =
                            "SELECT  CustomerID,  CompanyName,  ContactName,  Address  "  +
                            "FROM  Customers  "  +
                            "WHERE  CustomerID  =  'ALFKI'";

                        mySqlConnection.Open();
                        SqlDataReader  mySqlDataReader  =  mySqlCommand.ExecuteReader();

                        mySqlDataReader.Read();

                        Console.WriteLine("mySqlDataReader[\"  CustomerID\"]  =  "  +  mySqlDataReader["CustomerID"]);
                        Console.WriteLine("mySqlDataReader[\"  CompanyName\"]  =  "  +  mySqlDataReader["CompanyName"]);
                        Console.WriteLine("mySqlDataReader[\"  ContactName\"]  =  "  +  mySqlDataReader["ContactName"]);
                        Console.WriteLine("mySqlDataReader[\"  Address\"]  =  "  +  mySqlDataReader["Address"]);
                        mySqlDataReader.Close();

                        mySqlConnection.Close();
                }  catch  (SqlException  e)  {
                        Console.WriteLine("A  SqlException  was  thrown");
                        Console.WriteLine("Number  =  "  +  e.Number);
                        Console.WriteLine("Message  =  "  +  e.Message);
                        Console.WriteLine("StackTrace:\n"  +  e.StackTrace);
                }
        }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» SqlDataReader