Typed Accessors : SqlDataReader : ADO.Net C# Examples


C# Examples » ADO.Net » SqlDataReader »

 

Typed Accessors









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

class  MainClass
{
      static  void  Main(string[]  args)
      {
            string  connString  =  @"server  =  .\sqlexpress;integrated  security  =  true;database  =  northwind";

            string  sql  =  @"select  productname,unitprice,unitsinstock,discontinued  from  products";

            SqlConnection  conn  =  new  SqlConnection(connString);  

            try
            {
                  conn.Open();

                  SqlCommand  cmd  =  new  SqlCommand(sql,  conn);

                  SqlDataReader  rdr  =  cmd.ExecuteReader();

                  while  (rdr.Read())
                  {
                        Console.WriteLine(
                              "{0}\t  {1}\t\t  {2}\t  {3}",
                              //  nvarchar
                              rdr.GetString(0).PadRight(30),  
                              //  money
                              rdr.GetDecimal(1),
                              //  smallint
                              rdr.GetInt16(2),
                              //  bit
                              rdr.GetBoolean(3));
                  }
                  rdr.Close();
            }
            catch(Exception  e)
            {
                  Console.WriteLine("Error  Occurred:  "  +  e);
            }
            finally
            {
                  conn.Close();
            }
      }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» SqlDataReader