IDataRecord IDataRecord IDataRecord IDataRecord Interface

Definition

Provides access to the column values within each row for a DataReader, and is implemented by .NET Framework data providers that access relational databases.

public interface class IDataRecord
public interface IDataRecord
type IDataRecord = interface
Public Interface IDataRecord
Derived

Examples

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, and 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]));
    }

}
Option Explicit On
Option Strict On

Imports System.Data
Imports System.Data.SqlClient

Module Module1

    Sub Main()
        Dim str As String = "Data Source=(local);Initial Catalog=Northwind;" _
       & "Integrated Security=SSPI;"
        ReadOrderData(str)
    End Sub

    Private Sub ReadOrderData(ByVal connectionString As String)
        Dim queryString As String = _
            "SELECT OrderID, CustomerID FROM dbo.Orders;"

        Using connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand(queryString, connection)
            connection.Open()

            Dim reader As SqlDataReader = command.ExecuteReader()

            ' Call Read before accessing data.
            While reader.Read()
                ReadSingleRow(CType(reader, IDataRecord))
            End While

            ' Call Close when done reading.
            reader.Close()
        End Using
    End Sub

    Private Sub ReadSingleRow(ByVal record As IDataRecord)
       Console.WriteLine(String.Format("{0}, {1}", record(0), record(1)))

    End Sub

End Module

Remarks

The IDataReader and IDataRecord interfaces enable an inheriting class to implement a DataReader class. This provides a way 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 IDataRecord interface directly, but creates an instance of a class that inherits IDataRecord. Typically, you do this by obtaining a DataReader through the ExecuteReader method of the Command object.

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

Providers implementing a DataReader are required to expose data in common language runtime (CLR) types. Type coercion is defined for some types not included in the CLR. These values may be accessed as alternative types that comply with CLR types. As an example, the following table lists suggested mappings from OLE DB data types to CLR types, with alternative types in parentheses.

OLE DB type CLR type
DBTYPE_BOOL Int16
DBTYPE_BSTR string
DBTYPE_BYTES byte[]
DBTYPE_CY Decimal
DBTYPE_DATE DateTime
DBTYPE_DBDATE DateTime
DBTYPE_DBTIME DateTime
DBTYPE_DBTIMESTAMP DateTime
DBTYPE_DECIMAL Decimal
DBTYPE_EMPTY null
DBTYPE_ERROR ExternalException
DBTYPE_FILETIME DateTime
DBTYPE_GUID Guid
DBTYPE_HCHAPTER not supported
DBTYPE_I1 SByte
DBTYPE_I2 Int16
DBTYPE_I4 Int32
DBTYPE_I8 Int64
DBTYPE_IDISPATCH object
DBTYPE_IUNKNOWN object
DBTYPE_NULL DBNull.Value
DBTYPE_NUMERIC Decimal
DBTYPE_PROPVARIANT object
DBTYPE_R4 Single
DBTYPE_R8 Double
DBTYPE_STR string
DBTYPE_UDT not supported
DBTYPE_UI1 byte (Int16)
DBTYPE_UI2 UInt16 (Int32)
DBTYPE_UI4 UInt32 (Int64)
DBTYPE_UI8 UInt64 (Decimal)
DBTYPE_VARIANT object
DBTYPE_VARNUMERIC not supported
DBTYPE_WSTR string

Properties

FieldCount FieldCount FieldCount FieldCount

Gets the number of columns in the current row.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

Gets the column located at the specified index.

Item[String] Item[String] Item[String] Item[String]

Gets the column with the specified name.

Methods

GetBoolean(Int32) GetBoolean(Int32) GetBoolean(Int32) GetBoolean(Int32)

Gets the value of the specified column as a Boolean.

GetByte(Int32) GetByte(Int32) GetByte(Int32) GetByte(Int32)

Gets the 8-bit unsigned integer value of the specified column.

GetBytes(Int32, Int64, Byte[], Int32, Int32) GetBytes(Int32, Int64, Byte[], Int32, Int32) GetBytes(Int32, Int64, Byte[], Int32, Int32) GetBytes(Int32, Int64, Byte[], Int32, Int32)

Reads a stream of bytes from the specified column offset into the buffer as an array, starting at the given buffer offset.

GetChar(Int32) GetChar(Int32) GetChar(Int32) GetChar(Int32)

Gets the character value of the specified column.

GetChars(Int32, Int64, Char[], Int32, Int32) GetChars(Int32, Int64, Char[], Int32, Int32) GetChars(Int32, Int64, Char[], Int32, Int32) GetChars(Int32, Int64, Char[], Int32, Int32)

Reads a stream of characters from the specified column offset into the buffer as an array, starting at the given buffer offset.

GetData(Int32) GetData(Int32) GetData(Int32) GetData(Int32)

Returns an IDataReader for the specified column ordinal.

GetDataTypeName(Int32) GetDataTypeName(Int32) GetDataTypeName(Int32) GetDataTypeName(Int32)

Gets the data type information for the specified field.

GetDateTime(Int32) GetDateTime(Int32) GetDateTime(Int32) GetDateTime(Int32)

Gets the date and time data value of the specified field.

GetDecimal(Int32) GetDecimal(Int32) GetDecimal(Int32) GetDecimal(Int32)

Gets the fixed-position numeric value of the specified field.

GetDouble(Int32) GetDouble(Int32) GetDouble(Int32) GetDouble(Int32)

Gets the double-precision floating point number of the specified field.

GetFieldType(Int32) GetFieldType(Int32) GetFieldType(Int32) GetFieldType(Int32)

Gets the Type information corresponding to the type of Object that would be returned from GetValue(Int32).

GetFloat(Int32) GetFloat(Int32) GetFloat(Int32) GetFloat(Int32)

Gets the single-precision floating point number of the specified field.

GetGuid(Int32) GetGuid(Int32) GetGuid(Int32) GetGuid(Int32)

Returns the GUID value of the specified field.

GetInt16(Int32) GetInt16(Int32) GetInt16(Int32) GetInt16(Int32)

Gets the 16-bit signed integer value of the specified field.

GetInt32(Int32) GetInt32(Int32) GetInt32(Int32) GetInt32(Int32)

Gets the 32-bit signed integer value of the specified field.

GetInt64(Int32) GetInt64(Int32) GetInt64(Int32) GetInt64(Int32)

Gets the 64-bit signed integer value of the specified field.

GetName(Int32) GetName(Int32) GetName(Int32) GetName(Int32)

Gets the name for the field to find.

GetOrdinal(String) GetOrdinal(String) GetOrdinal(String) GetOrdinal(String)

Return the index of the named field.

GetString(Int32) GetString(Int32) GetString(Int32) GetString(Int32)

Gets the string value of the specified field.

GetValue(Int32) GetValue(Int32) GetValue(Int32) GetValue(Int32)

Return the value of the specified field.

GetValues(Object[]) GetValues(Object[]) GetValues(Object[]) GetValues(Object[])

Populates an array of objects with the column values of the current record.

IsDBNull(Int32) IsDBNull(Int32) IsDBNull(Int32) IsDBNull(Int32)

Return whether the specified field is set to null.

Applies to