Jump to content

DataReader

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Taladon (talk | contribs) at 18:54, 28 June 2006 (Initial File). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

In .NET, a DataReader object is used to seqentially read data from a data source of some 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());
}

}