Use SqlDataReader to retrieve data in the resultset : SqlDataReader : ADO.Net C# Examples


C# Examples » ADO.Net » SqlDataReader »

 

Use SqlDataReader to retrieve data in the resultset









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

class  MainClass
{
    static  void  Main(string[]  args)
    {
                string  SQL  =  "SELECT  *  FROM  Employee";

        string  ConnectionString  ="server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated  Security=SSPI;";
        SqlConnection  conn  =  new  SqlConnection(ConnectionString);

                SqlCommand  cmd  =  new  SqlCommand(SQL,  conn);

                conn.Open();
                SqlDataReader  reader  =  cmd.ExecuteReader();
                try  
                {
                        while  (reader.Read())  
                        {
                                Console.Write("ID:"+reader.GetInt32(0).ToString()  );
                                Console.Write("  ,");
                                Console.WriteLine("Name:"  +  reader.GetString(1).ToString()  );
                        }
                }
                finally  
                {
                        reader.Close();
                        conn.Close();
                }      
    }
}
    
   
  
   



Output

ID:2 ,Name:G


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» SqlDataReader