Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Using a DataReader to read row by row

using System; using System.Data; using System.Data.SqlClient; class MainClass {   static void Main(string[] args)   {     SqlConnection MyConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");     MyConnection.Open();     SqlCommand MyCommand = new SqlCommand("SELECT * FROM Employee", MyConnection);     SqlDataReader MyDataReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);     while (MyDataReader.Read())     {       Console.WriteLine(MyDataReader[0] + " " + MyDataReader[1]);     }     MyConnection.Close();   } }