SqlDataReader Class

System.Data.SqlClient
Description: Represents a row-access data stream for SQL Server data. This class cannot be inherited.

Syntax


public sealed class SqlDataReader : DbDataReader, IDisposable
                

Inheritance Hierarchy

System.Object
  System.MarshalByRefObject
    System.Data.Common.DbDataReader<T>
      System.Data.Common.DbDataReader
        System.Data.SqlClient.SqlDataReader
            

Interfaces

Remarks

The SqlDataReader provides a non-buffered, forward-only stream of data rows from a SQL Server result set. When you use SqlDataReader, you retrieve data one row at a time, and you must close the reader when the operations are complete. Using SqlDataReader is efficient because it reads the data as the server sends it.

Use the ExecuteReader method of the SqlCommand object to create an instance of SqlDataReader.

The SqlDataReader exposes column data as values of the appropriate .NET Framework type. You can also retrieve values as a String, Stream, or byte[]. SqlDataReader supports reading all data types, including large object types (LOBs), such as varchar(max), nvarchar(max), and varbinary(max).

The SqlDataReader can be used to retrieve binary data from a database by calling the GetBytes or GetChars methods.

Methods

Name Description
GetBoolean Gets the value of the specified column as a Boolean.
GetByte Gets the value of the specified column as a byte.
GetBytes Reads a stream of bytes from the specified column offset to the end of the buffer.
GetChar Gets the value of the specified column as a character.
GetChars Reads a stream of characters from the specified column offset to the end of the buffer.
GetDateTime Gets the value of the specified column as a DateTime object.
GetDecimal Gets the value of the specified column as a Decimal.
GetDouble Gets the value of the specified column as a double-precision floating-point number.
GetGuid Gets the value of the specified column as a GUID.
GetInt16 Gets the value of the specified column as a 16-bit signed integer.
GetInt32 Gets the value of the specified column as a 32-bit signed integer.
GetInt64 Gets the value of the specified column as a 64-bit signed integer.
GetName Gets the name of the specified column.
GetString Gets the value of the specified column as a string.
GetStream Gets the value of the specified column as a Stream.
Close Closes the SqlDataReader object.
Read Advances the SqlDataReader to the next record.
HasRows Gets a value indicating whether the data reader contains one or more rows.

Properties

Name Description
Depth Gets the depth of the current row in the result set.
FieldCount Gets the number of columns in the current row.
IsClosed Gets a value indicating whether the SqlDataReader has been closed.
Item Gets the value of the column at the specified index.
VisibleFieldCount Gets the number of columns in the current row that are not hidden.

Example


using System;
using System.Data;
using System.Data.SqlClient;

public class Example
{
    public static void ReadData(string connectionString)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            string sql = "SELECT TOP 5 CustomerID, CompanyName, ContactName FROM Customers";
            using (SqlCommand command = new SqlCommand(sql, connection))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            // Access data by column index
                            Console.WriteLine($"- ID: {reader.GetInt32(0)}, Company: {reader.GetString(1)}, Contact: {reader.GetString(2)}");

                            // Access data by column name
                            // Console.WriteLine($"- ID: {reader["CustomerID"]}, Company: {reader["CompanyName"]}, Contact: {reader["ContactName"]}");
                        }
                    }
                    else
                    {
                        Console.WriteLine("No rows found.");
                    }
                }
            }
        }
    }
}
                

Related Topics