DataReaders allow you to access a database in a fast and efficient fashion by providing read-only and forward-only access. The beauty of DataReaders is that cursor types and lock types are no longer required. The following example demonstrates using a DataReader.
using System;
using System.Data;
using System.Data.SqlClient;
namespace Client.Chapter_13___ADO.NET
{
class UsingADataReader
{
static void Main(string[] args)
{
SqlConnection MyConnection =
new SqlConnection(@"Data Source=(local);"
+ "Initial Catalog = CaseManager;"
+ "Integrated Security=true");
MyConnection.Open();
SqlCommand MyCommand =
new SqlCommand("SELECT * FROM CaseInfo", MyConnection);
SqlDataReader MyDataReader =
MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
while (MyDataReader.Read())
{
Console.WriteLine(MyDataReader[0] + " "
+ MyDataReader[1]);
}
MyConnection.Close();
}
}
}