DataReader

From Wikipedia, the free encyclopedia

In ADO.NET, a DataReader object is used to sequentially read data from a data source of any kind. Two common data sources used are SQL Servers via System.Data.SqlClient or OLE DB via System.Data.OleDb.

A DataReader is usually accompanied by a Command object that contains information about the query and which connection object to query on.


Sample of accessing SQL Data using DataReader

void DataTest()
{
 SqlConnection conn1 = new SqlConnection("Initial_Catalog=test,Integrated_Security=SSPI");
 conn1.Open()
 SqlCommand mycommand = new SqlCommand("select * from test", conn1);
 SqlDataReader myreader = mycommand.ExecuteReader();
 while(myreader.Read())
 {
   Console.WriteLine(myreader.GetValue(0).ToString());
 }
 myreader.Close();
 mycommand.Dispose(); 
 conn1.Dispose();
}