IDataRecord 接口

定义

DataReader 提供对每行中的列值的访问权限,并由访问关系数据库的.NET Framework 数据提供程序实现。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
派生

示例

下面的示例创建派生类、 SqlConnection SqlCommand、和SqlDataReader的实例。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. 最后, 该示例关闭SqlDataReader, SqlConnection然后关闭。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

注解

和接口使继承类可实现类。DataReader IDataRecord IDataReaderThe 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. 有关DataReader类的详细信息, 请参阅使用 DataReader 检索数据For more information about DataReader classes, see Retrieving Data Using a DataReader.

应用程序不会直接创建IDataRecord接口的实例, 而是创建继承IDataRecord的类的实例。An application does not create an instance of the IDataRecord interface directly, but creates an instance of a class that inherits IDataRecord. 通常, 通过获取DataReader ExecuteReader Command对象的方法来执行此操作。Typically, you do this by obtaining a DataReader through the ExecuteReader method of the Command object.

继承IDataRecord的类必须实现所有继承成员, 并且通常定义其他成员来添加特定于提供程序的功能。Classes that inherit IDataRecord must implement all inherited members, and typically define additional members to add provider-specific functionality.

实现的提供DataReader程序需要公开公共语言运行时 (CLR) 类型中的数据。Providers implementing a DataReader are required to expose data in common language runtime (CLR) types. 为不包含在 CLR 中的某些类型定义类型强制。Type coercion is defined for some types not included in the CLR. 这些值可以作为符合 CLR 类型的备用类型进行访问。These values may be accessed as alternative types that comply with CLR types. 作为示例, 下表列出了建议从 OLE DB 数据类型映射到 CLR 类型的映射, 并将替代类型括在括号中。As an example, the following table lists suggested mappings from OLE DB data types to CLR types, with alternative types in parentheses.

OLE DB 类型OLE DB type CLR 类型CLR type
DBTYPE_BOOLDBTYPE_BOOL Int16Int16
DBTYPE_BSTRDBTYPE_BSTR stringstring
DBTYPE_BYTESDBTYPE_BYTES byte[]byte[]
DBTYPE_CYDBTYPE_CY 十进制Decimal
DBTYPE_DATEDBTYPE_DATE DateTimeDateTime
DBTYPE_DBDATEDBTYPE_DBDATE DateTimeDateTime
DBTYPE_DBTIMEDBTYPE_DBTIME DateTimeDateTime
DBTYPE_DBTIMESTAMPDBTYPE_DBTIMESTAMP DateTimeDateTime
DBTYPE_DECIMALDBTYPE_DECIMAL 十进制Decimal
DBTYPE_EMPTYDBTYPE_EMPTY nullnull
DBTYPE_ERRORDBTYPE_ERROR ExternalExceptionExternalException
DBTYPE_FILETIMEDBTYPE_FILETIME DateTimeDateTime
DBTYPE_GUIDDBTYPE_GUID GUIDGuid
DBTYPE_HCHAPTERDBTYPE_HCHAPTER 不受支持not supported
DBTYPE_I1DBTYPE_I1 SByteSByte
DBTYPE_I2DBTYPE_I2 Int16Int16
DBTYPE_I4DBTYPE_I4 Int32Int32
DBTYPE_I8DBTYPE_I8 Int64Int64
DBTYPE_IDISPATCHDBTYPE_IDISPATCH 对象 (object)object
DBTYPE_IUNKNOWNDBTYPE_IUNKNOWN 对象 (object)object
DBTYPE_NULLDBTYPE_NULL DBNull.ValueDBNull.Value
DBTYPE_NUMERICDBTYPE_NUMERIC 十进制Decimal
DBTYPE_PROPVARIANTDBTYPE_PROPVARIANT 对象 (object)object
DBTYPE_R4DBTYPE_R4 SingleSingle
DBTYPE_R8DBTYPE_R8 DoubleDouble
DBTYPE_STRDBTYPE_STR stringstring
DBTYPE_UDTDBTYPE_UDT 不受支持not supported
DBTYPE_UI1DBTYPE_UI1 byte (Int16)byte (Int16)
DBTYPE_UI2DBTYPE_UI2 UInt16 (Int32)UInt16 (Int32)
DBTYPE_UI4DBTYPE_UI4 UInt32 (Int64)UInt32 (Int64)
DBTYPE_UI8DBTYPE_UI8 UInt64 (十进制)UInt64 (Decimal)
DBTYPE_VARIANTDBTYPE_VARIANT 对象 (object)object
DBTYPE_VARNUMERICDBTYPE_VARNUMERIC 不受支持not supported
DBTYPE_WSTRDBTYPE_WSTR stringstring

属性

FieldCount

获取当前行中的列数。Gets the number of columns in the current row.

Item[Int32]

获取位于指定索引处的列。Gets the column located at the specified index.

Item[String]

获取具有指定名称的列。Gets the column with the specified name.

方法

GetBoolean(Int32)

获取指定列的布尔值形式的值。Gets the value of the specified column as a Boolean.

GetByte(Int32)

获取指定列的 8 位无符号整数值。Gets the 8-bit unsigned integer value of the specified column.

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)

获取指定列的字符值。Gets the character value of the specified column.

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)

返回指定列序号的 IDataReaderReturns an IDataReader for the specified column ordinal.

GetDataTypeName(Int32)

获取指定字段的数据类型信息。Gets the data type information for the specified field.

GetDateTime(Int32)

获取指定字段的日期和时间数据值。Gets the date and time data value of the specified field.

GetDecimal(Int32)

获取指定字段的固定位置数值。Gets the fixed-position numeric value of the specified field.

GetDouble(Int32)

获取指定字段的双精度浮点数。Gets the double-precision floating point number of the specified field.

GetFieldType(Int32)

获取对应于会从 GetValue(Int32) 返回的 Object 类型的 Type 信息。Gets the Type information corresponding to the type of Object that would be returned from GetValue(Int32).

GetFloat(Int32)

获取指定字段的单精度浮点数。Gets the single-precision floating point number of the specified field.

GetGuid(Int32)

返回指定字段的 GUID 值。Returns the GUID value of the specified field.

GetInt16(Int32)

获取指定字段的 16 位带符号整数值。Gets the 16-bit signed integer value of the specified field.

GetInt32(Int32)

获取指定字段的 32 位带符号整数值。Gets the 32-bit signed integer value of the specified field.

GetInt64(Int32)

获取指定字段的 64 位带符号整数值。Gets the 64-bit signed integer value of the specified field.

GetName(Int32)

获取要查找的字段的名称。Gets the name for the field to find.

GetOrdinal(String)

返回已命名字段的索引。Return the index of the named field.

GetString(Int32)

获取指定字段的字符串值。Gets the string value of the specified field.

GetValue(Int32)

返回指定字段的值。Return the value of the specified field.

GetValues(Object[])

使用当前记录的列值填充对象的数组。Populates an array of objects with the column values of the current record.

IsDBNull(Int32)

返回指定字段是否设置为 null。Return whether the specified field is set to null.

适用于