Use While loop to read query result data from SqlDataReader : SqlDataReader : Database C# Source Code


Custom Search

C# Source Code » Database » SqlDataReader »

 

Use While loop to read query result data from SqlDataReader








    

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

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

         string sql = @"select FirstName from Employee";

         SqlConnection conn = new SqlConnection(connString);

         try {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while(reader.Read()) {
               Console.WriteLine("{0}", reader[0]);
            }
            reader.Close();
         }
         catch(Exception e)
         {
            Console.WriteLine("Error Occurred: " + e);
         }
         finally
         {
            conn.Close();
         }
      }
   } 


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Database
» SqlDataReader