ADO.NET SqlDataReader Class
This class is used to read data from SQL Server database. It reads data in forward-only stream of rows from a SQL Server database. it is sealed class so that cannot be inherited. It inherits DbDataReader class and implements IDisposable interface.
SqlDataReader Signature
SqlDataReader Properties
Property |
Description |
Connection |
It is used to get the SqlConnection associated with the SqlDataReader. |
Depth |
It is used to get a value that indicates the depth of nesting for the current row. |
FieldCount |
It is used to get the number of columns in the current row. |
HasRows |
It is used to get a value that indicates whether the SqlDataReader contains one or more rows. |
IsClosed |
It is used to retrieve a boolean value that indicates whether the specified SqlDataReader instance has been closed. |
Item[String] |
It is used to get the value of the specified column in its native format given the column name. |
Item[Int32] |
It is used to get the value of the specified column in its native format given the column ordinal. |
RecordsAffected |
It is used to get the number of rows changed, inserted or deleted by execution of the Transact-SQL statement. |
VisibleFieldCount |
It is used to get the number of fields in the SqlDataReader that are not hidden. |
Methods
Method |
Description |
Close() |
It is used to closes the SqlDataReader object. |
GetBoolean(Int32) |
It is used to get the value of the specified column as a Boolean. |
GetByte(Int32) |
It is used to get the value of the specified column as a byte. |
GetChar(Int32) |
It is used to get the value of the specified column as a single character. |
GetDateTime(Int32) |
It is used to get the value of the specified column as a DateTime object. |
GetDecimal(Int32) |
It is used to get the value of the specified column as a Decimal object. |
GetDouble(Int32) |
It is used to get the value of the specified column as a double-precision floating point number. |
GetFloat(Int32) |
It is used to get the value of the specified column as a single-precision floating point number. |
GetName(Int32) |
It is used to get the name of the specified column. |
GetSchemaTable() |
It is used to get a DataTable that describes the column metadata of the SqlDataReader. |
GetValue(Int32) |
It is used to get the value of the specified column in its native format. |
GetValues(Object[]) |
It is used to populate an array of objects with the column values of the current row. |
NextResult() |
It is used to get the next result, when reading the results of SQL statements. |
Read() |
It is used to read record from the SQL Server database. |
To create a SqlDataReader instance, we must call the ExecuteReader method of the SqlCommand object.
Example
In the following program, we are using SqlDataReader to get data from the SQL Server. A C# code is given below.
// Program.cs
Output:
Execute this program by combination of Ctrl+F5 and it will produce the following output.
|