SqlDataReader Class in ADO
SqlDataReader Class in ADO
ADO.Net
PRESENTER: MOHAMMAD ADIL
A DO.Net SqlDataReader Class With Different Data Providers
Signature SqlDataReader
ADO.Net SqlDataReader Class
• It means you can only read and display data but can’t update or
delete data.
• If you want to make modification in retrieved data you need to use
DataAdapter instead of DataReader.
• SqlDataReader Object provides a connection oriented data access
to the SQL Server data Sources from C# applications.
• ExecuteReader() in the SqlCommand Object sends the SQL
statements to the SqlConnection Object and populate a
SqlDataReader Object based on the SQL statement or Stored
Procedures.
ADO.Net SqlDataReader Class
• Object of SqlDataReader cannot be created using new keyword.
• SqlDataReader dr = new SqlDataReader();
• ExecuteReader() method of SqlCommand create and returns an
object of SqlDataReader.
• SqlDataReader is read-only. It means it is also not possible to
change the data using SqlDataReader.
• You also need to open and close the connection explicitly.
WHY AND WHEN TO USE DATAREADER?
• When you want to only display information or search result, you can
use DataReader. There are various advantages of using DataReader
like:
• 1. The retrieved data is stored in the network buffer in the client
and then the client can read data using Read method. As data gets
stored in the client network buffer it increases application
performance significantly.
• 2. By default DataReader stores only one row at a time in memory.
It reduces system overhead.
Properties Of SqlDataReader Class
• FieldCount (int): It gets the number of columns in the current row.
• HasRows (boolean): It gets a value that indicates whether the
System.Data.SqlClient.SqlDataReader contains one or more rows.
• IsClosed: It retrieves a Boolean value that indicates whether the
specified System.Data.SqlClient.SqlDataReader instance has been
closed
• Item[String]: It gets the value of the specified column in its native
format given the column name.
• Item[Int32]: It gets the value of the specified column in its native
format given the column ordinal.
Methods Of SqlDataReader Class
• Read(): It Advances the System.Data.SqlClient.SqlDataReader to the
next record and returns true if there are more rows; otherwise false
You can read data by using concatenation and placeholder syntax in
console application.
• GetName(int i): It gets the name of the specified column. Here,
parameter i is the zero-based column ordinal.
• NextResult(): It advances the data reader to the next result when
reading the results of batch Transact-SQL statements.