IDatabaseReader Interface
The IDatabaseReader interface provides a forward‑only, read‑only cursor for accessing rows from a data source. It is typically implemented by data providers that expose relational data.
Namespace
System.Data
Assembly
System.Data.Common.dll
Implements
Properties
| Property | Type | Description |
|---|---|---|
Depth | int | Gets the level of nesting for the current row. |
FieldCount | int | Gets the number of columns in the current row. |
IsClosed | bool | Indicates whether the reader is closed. |
RecordsAffected | int | Number of rows changed, inserted, or deleted by execution of the SQL statement. |
VisibleFieldCount | int | Number of visible columns for the current row. |
Methods
| Method | Signature | Description |
|---|---|---|
Close() | void Close() | Closes the data reader. |
GetBoolean(int) | bool GetBoolean(int ordinal) | Retrieves the value of the specified column as a Boolean. |
GetInt32(int) | int GetInt32(int ordinal) | Retrieves the value of the specified column as a 32‑bit integer. |
GetString(int) | string GetString(int ordinal) | Retrieves the value of the specified column as a string. |
Read() | bool Read() | Advances the reader to the next record. |
GetValue(int) | object GetValue(int ordinal) | Retrieves the value of the specified column as an object. |
IsDBNull(int) | bool IsDBNull(int ordinal) | Determines whether the column contains non-existent or missing values. |
Example
// Example: Using IDatabaseReader to read data
using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = "Server=.;Database=SampleDB;Trusted_Connection=True;";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (IDatabaseReader reader = ExecuteCommand(conn, "SELECT Id, Name FROM Employees"))
{
while (reader.Read())
{
int id = reader.GetInt32(0);
string name = reader.GetString(1);
Console.WriteLine($"Id: {id}, Name: {name}");
}
}
}
}
static IDatabaseReader ExecuteCommand(SqlConnection conn, string sql)
{
SqlCommand cmd = new SqlCommand(sql, conn);
return cmd.ExecuteReader();
}
}