Hi,
Ques What's the role of the DataReader class in ADO.NET connections ?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Mar 26, 2012, 10:47 AM EDT
SenthilkumarRank #387 · 6.5K reputationPosted Mar 26, 2012, 8:02 PM EDT
The SqlDataReader is class in System.Data.SqlClient namespace, which is used to read the data from the sql server in connected database.
It is connection oriented architecture and it reads the underleying connected database in demand basis. It is read only, forward only and it is fast to retrieve the data.
Microsoft suggest to use the DataReader because it read only and reads the record at a time, minimum usage of the memory.
It can go front and back because it does not have the cursor. It cannot write the data because it is read only.
This class cannot be inherited because it is sealed by default. In order to fill the data in the DataReader class, we need to execute the ExecuteReader() method in SqlCommand object.
For more info:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 26, 2012, 8:24 PM EDT
ADO.NET DataReader is used to retrieve a read-only, forward-only stream of data from a database. Results are returned as the query executes, and are stored in the network buffer on the client until you request them using the Read method of the DataReader.
Changes made to a result set by another process or thread while data is being read may be visible to the user of the SqlDataReader. the precise behavior is timing dependent.
please referhttp://msdn.microsoft.com/en-us/library/haa3afyz.aspx
working with datareader.
Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 26, 2012, 8:58 PM EDT
The SqlDataReader class defines a lightweight yet powerful object that is used to read information from a SQL database. A SqlDataReader object has a small footprint, because it doesn't contain more than a single record in memory at any time. This makes it ideal for reading large amounts of data from a database.DataReader represents only one database record at a time. You must call the Read() method to fetch each new record from the underlying database table into memory. Each time you call Read() again, the previously fetched record is lost. DataReader must remain connected to a database table. A DataReader is tied down to its underlying data source. It returns a forward-only, read-only view of datafrom the data source when the command is executed.
Properties and Methods of the SqlDataReader Class
SqlDataReader.FieldCount
Syntax
Description
The Fieldcount property contains the number of fields in the current record.
Example
int FieldCount; //Instantiate and open connection object SqlConnection conn = new SqlConnection("Data Source=localhost; User Id=sa; Password=; Initial Catalog=northwind"); conn.Open(); //Get command object SqlCommand cmd = new SqlCommand("Select employee_id, lastname, firstname from Employees", conn); //Execute reader SqlDataReader reader = cmd.ExecuteReader(); //Get FieldCount FieldCount = reader.FieldCount;SqlDataReader.IsClosed
Syntax
Description
The IsClosed property contains True if the SqlDataReader object is closed.
Example
SqlDataReader.Item
Syntax
Description
The Item property retrieves the value of a column in its native data format.
Example
string message = ""; //Instantiate and open connection object SqlConnection conn = new SqlConnection("Data Source=localhost; User Id=sa; Password=; Initial Catalog=northwind"); conn.Open(); //Get command object SqlCommand cmd = new SqlCommand("Select employee_id, lastname, firstname from Employees", conn); //Execute reader SqlDataReader reader = cmd.ExecuteReader(); //Loop through all records building a string while(reader.Read()){ message = message + reader["employee_id"] + " " + reader["lastname"] + ", " + reader["firstname"] + " "; } reader.Close();SqlDataReader.RecordsAffected
Syntax
Description
The RecordsAffected property contains the number of records affected by the query.
Example
int RecordsAffected; //Instantiate and open connection object SqlConnection conn = new SqlConnection("Data Source=localhost; User Id=sa; Password=; Initial Catalog=northwind"); conn.Open(); //Get command object SqlCommand cmd = new SqlCommand("Select employee_id, lastname, firstname from Employees", conn); //Execute reader SqlDataReader reader = cmd.ExecuteReader(); //Get RecordsAffected RecordsAffected = reader.RecordsAffected;SqlDataReader.Close()
Syntax
Description
The Close() method closes the SqlDataReader object.
Example
SqlDataReader.GetBoolean()
Syntax
Description
The GetBoolean() method returns the value of a specified column as type Boolean.
Example
SqlDataReader.GetByte
Syntax
Description
The GetByte() method returns the value of a specified column as type Byte.
Example
SqlDataReader.GetBytes
Syntax
Description
The GetBytes() method returns the value of a specified column as type Bytes.
Example
SqlDataReader.GetChar()
Syntax
Description
The GetChar() method returns the value of a specified column as type Char.
Example
SqlDataReader.GetChars()
Syntax
Description
The GetChars() method returns the value of a specified column as type Char.
Example
SqlDataReader.GetDataTypeName()
Syntax
Description
The GetDataTypeName() method returns a string that contains the data type of the specified field.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost; User Id=sa; Password=; Initial Catalog=northwind"); conn.Open(); //Get command object SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn); //Execute reader SqlDataReader reader = cmd.ExecuteReader(); string datatype = reader.GetDataTypeName(0); reader.Close();SqlDataReader.GetDateTime()
Syntax
Description
The GetDateTime() method returns the value of a specified column as type DateTime.
Example
SqlDataReader.GetDecimal()
Syntax
Description
The GetDecimal() method returns the value of a specified column as type Decimal.
Example
SqlDataReader.GetDouble()
Syntax
Description
The GetDouble() method returns the value of a specified column as type Double.
Example
SqlDataReader.GetFieldType()
Syntax
Description
The GetFieldType() method returns the type of the specified field.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost; User Id=sa; Password=; Initial Catalog=northwind"); conn.Open(); //Get command object SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn); //Execute reader SqlDataReader reader = cmd.ExecuteReader(); Type dataType = reader.GetFieldType(0); reader.Close();SqlDataReader.GetFloat()
Syntax
Description
The GetFloat() method returns the value of a specified column as type Float.
Example
SqlDataReader.GetGuid()
Syntax
Description
The GetGuid() method returns the value of a specified column as type Guid.
Example
SqlDataReader.GetInt16()
Syntax
Description
The GetInt16() method returns the value of a specified column as type Int16.
Example
SqlDataReader.GetInt32()
Syntax
Description
The GetInt32() method returns the value of a specified column as type Int32.
Example
SqlDataReader.GetInt64()
Syntax
Description
The GetInt64() method returns the value of a specified column as type Int64.
Example
SqlDataReader.GetName()
Syntax
Description
The GetName() method returns a string that contains the name of the SQL field.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost; User Id=sa; Password=; Initial Catalog=northwind"); conn.Open(); //Get command object SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn); //Execute reader SqlDataReader reader = cmd.ExecuteReader(); string fieldname = reader.GetName(0); reader.Close();SqlDataReader.GetOrdinal()
Syntax
Description
The GetOrdinal() method returns an integer that contains the ordinal number of the specified field.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost; User Id=sa; Password=; Initial Catalog=northwind"); conn.Open(); //Get command object SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn); //Execute reader SqlDataReader reader = cmd.ExecuteReader(); int fieldOrdinal = reader.GetOrdinal(fieldname); reader.Close();SqlDataReader.GetSchemaTable()
Syntax
Description
The GetSchemaTable() method returns a data table that contains database schema for the SqlCommand object.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost; User Id=sa; Password=; Initial Catalog=northwind"); conn.Open(); //Get command object SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn); //Execute reader SqlDataReader reader = cmd.ExecuteReader(); DataTable aTable = reader.GetSchemaTable(); reader.Close();SqlDataReader.GetSqlBinary()
Syntax
Description
The GetSqlBinary() method returns the value of a specified column as type SqlBinary.
Example
SqlDataReader.GetSqlBit()
Syntax
Description
The GetSqlBit() method returns the value of a specified column as type SqlBit.
Example
SqlDataReader.GetSqlByte()
Syntax
Description
The GetSqlByte() method returns the value of a specified column as type SqlByte.
Example
SqlDataReader.GetSqlDateTime()
Syntax
Description
The GetSqlDateTime() method returns the value of a specified column as type SqlDateTime.
Example
SqlDataReader.GetSqlDecimal()
Syntax
Description
The GetSqlDecimal() method returns the value of a specified column as type SqlDecimal.
Example
SqlDataReader.GetSqlDouble()
Syntax
Description
The GetSqlDouble() method returns the value of a specified column as type SqlDouble.
Example
SqlDataReader.GetSqlGuid()
Syntax
Description
The GetSqlGuid() method returns the value of a specified column as type SqlGuid.
Example
SqlDataReader.GetSqlInt16()
Syntax
Description
The GetSqlInt16() method returns the value of a specified column as type SqlInt16.
Example
SqlDataReader.GetSqlInt32()
Syntax
Description
The GetSqlInt32() method returns the value of a specified column as type SqlInt32.
Example
SqlDataReader.GetSqlInt64()
Syntax
Description
The GetSqlInt64() method returns the value of a specified column as type SqlInt64.
Example
SqlDataReader.GetSqlMoney()
Syntax
Description
The GetSqlMoney() method returns the value of a specified column as type SqlMoney.
Example
SqlDataReader.GetSqlSingle()
Syntax
Description
The GetSqlSingle() method returns the value of a specified column as type SqlSingle.
Example
SqlDataReader.GetSqlString()
Syntax
Description
The GetSqlString() method returns the value of a specified column as type SqlString.
Example
SqlDataReader.GetSqlValue()
Syntax
Description
The GetSqlValue() method returns a value of type Object, by using its native SQL data type.
Example
SqlDataReader.GetString()
Syntax
Description
The GetString() method returns the value of a specified column as type String.
Example
SqlDataReader.GetValue()
Syntax
Description
The GetValue() method returns the value of type Object, by using the Microsoft .NET framework types.
Example
SqlDataReader.IsDBNull()
Syntax
Description
The IsDBNull() method returns True if the specified column is null and False otherwise.
Example
SqlDataReader.NextResult()
Syntax
Description
The NextResult() method advances the data reader to the next record. It is used when reading the result of SQL batch statements.
Example
SqlDataReader.Read()
Syntax
Description
The Read() method advances the data reader to the next record. It returns True if there are more records and False otherwise.
Example