Export (0) Print
Expand All

IDataReader Interface

Provides a means of reading one or more forward-only streams of result sets obtained by executing a command at a data source, and is implemented by .NET Framework data providers that access relational databases.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

public interface IDataReader : IDisposable, 
	IDataRecord

The IDataReader type exposes the following members.

  NameDescription
Public propertySupported by the XNA FrameworkDepthGets a value indicating the depth of nesting for the current row.
Public propertySupported by the XNA FrameworkFieldCountGets the number of columns in the current row. (Inherited from IDataRecord.)
Public propertySupported by the XNA FrameworkIsClosedGets a value indicating whether the data reader is closed.
Public propertySupported by the XNA FrameworkItem[Int32]Gets the column located at the specified index. (Inherited from IDataRecord.)
Public propertySupported by the XNA FrameworkItem[String]Gets the column with the specified name. (Inherited from IDataRecord.)
Public propertySupported by the XNA FrameworkRecordsAffectedGets the number of rows changed, inserted, or deleted by execution of the SQL statement.
Top

  NameDescription
Public methodSupported by the XNA FrameworkCloseCloses the IDataReader Object.
Public methodSupported by the XNA FrameworkDisposePerforms application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. (Inherited from IDisposable.)
Public methodSupported by the XNA FrameworkGetBooleanGets the value of the specified column as a Boolean. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetByteGets the 8-bit unsigned integer value of the specified column. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetBytesReads a stream of bytes from the specified column offset into the buffer as an array, starting at the given buffer offset. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetCharGets the character value of the specified column. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetCharsReads a stream of characters from the specified column offset into the buffer as an array, starting at the given buffer offset. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetDataReturns an IDataReader for the specified column ordinal. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetDataTypeNameGets the data type information for the specified field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetDateTimeGets the date and time data value of the specified field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetDecimalGets the fixed-position numeric value of the specified field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetDoubleGets the double-precision floating point number of the specified field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetFieldTypeGets the Type information corresponding to the type of Object that would be returned from GetValue. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetFloatGets the single-precision floating point number of the specified field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetGuidReturns the GUID value of the specified field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetInt16Gets the 16-bit signed integer value of the specified field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetInt32Gets the 32-bit signed integer value of the specified field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetInt64Gets the 64-bit signed integer value of the specified field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetNameGets the name for the field to find. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetOrdinalReturn the index of the named field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetSchemaTableReturns a DataTable that describes the column metadata of the IDataReader.
Public methodSupported by the XNA FrameworkGetStringGets the string value of the specified field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetValueReturn the value of the specified field. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkGetValuesPopulates an array of objects with the column values of the current record. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkIsDBNullReturn whether the specified field is set to null. (Inherited from IDataRecord.)
Public methodSupported by the XNA FrameworkNextResultAdvances the data reader to the next result, when reading the results of batch SQL statements.
Public methodSupported by the XNA FrameworkReadAdvances the IDataReader to the next record.
Top

The IDataReader and IDataRecord interfaces allow an inheriting class to implement a DataReader class, which provides a means of reading one or more forward-only streams of result sets. For more information about DataReader classes, see Retrieving Data Using a DataReader.

An application does not create an instance of the IDataReader interface directly, but creates an instance of a class that inherits IDataReader.

Classes that inherit IDataReader must implement the inherited members, and typically define additional members to add provider-specific functionality.

Changes made to a result set by another process or thread while data is being read may be visible to the user of a class that implements an IDataReader. However, the precise behavior is both provider and timing dependent.

Notes to Implementers

To promote consistency among .NET Framework data providers, name the inheriting class in the form Prv Command where Prv is the uniform prefix given to all classes in a specific .NET Framework data provider namespace. For example, Sql is the prefix of the SqlDataAdapter class in the System.Data.SqlClient namespace.

Users do not create an instance of a DataReader class directly. Instead, they obtain the DataReader instance through the ExecuteReader method of the Command object. Therefore, you should mark DataReader constructors as internal.

The following example creates instances of the derived classes, SqlConnection, SqlCommand, and SqlDataReader. The example reads through the data, writing it out to the console. Finally, the example closes the SqlDataReader, then the SqlConnection.

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


class Program
{
    static void Main()
    {
        string str = "Data Source=(local);Initial Catalog=Northwind;"
            + "Integrated Security=SSPI";
        ReadOrderData(str);
    }

    private static void ReadOrderData(string connectionString)
    {
        string queryString =
            "SELECT OrderID, CustomerID FROM dbo.Orders;";

        using (SqlConnection connection =
                   new SqlConnection(connectionString))
        {
            SqlCommand command =
                new SqlCommand(queryString, connection);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            // Call Read before accessing data. 
            while (reader.Read())
            {
                ReadSingleRow((IDataRecord)reader);
            }

            // Call Close when done reading.
            reader.Close();
        }
    }

    private static void ReadSingleRow(IDataRecord record)
    {
        Console.WriteLine(String.Format("{0}, {1}", record[0], record[1]));
    }

}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft