articles

Home / DeveloperSection / Articles / DataReader

DataReader

Amit Singh9280 04-Sep-2010

We can use DataReader in ADO.Net to retrieve a read-only, forward only stream of data from the database. Results are returned as the query executes Using the Data Reader can increase application performance both by retrieving data as soon as it is available, rather than waiting for the entire results of the query to be returned, and (by default) storing only one row at a time in memory, reducing system overhead. We cannot edit data in DataReader. After creating an instance of the Command object, you create a DataReader by calling Command.ExecuteReader () to retrieve rows from a data source.

The following example shows DataReader in c#. 
//these lines are for creating a new connection by passing server name,uid,password and name of the database as here mind.
 
SqlConnection con = newSqlConnection("server=uttam-pc1\\sqlexpress; uid=sa; password=sa; database=mind");
//open connection      
con.Open();
string strQuery=”SELECT * FROM tblEmployee”;
SqlCommand cmd = new SqlCommand(strQuery,con);
 
//created datareader object
 SqlDataReader dr = cmd.ExecuteReader();  
 dr.read();
 txtID.Text=dr[0].tostring();
 con.close();//closing the connection

Updated 04-Mar-2020

Leave Comment

Comments

Liked By