Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

IDataRecord Interface

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

For a list of all members of this type, see IDataRecord Members.

[Visual Basic]
Public Interface IDataRecord
[C#]
public interface IDataRecord
[C++]
public __gc __interface IDataRecord
[JScript]
public interface IDataRecord

Classes that Implement IDataRecord

Class Description
DbDataRecord Implements IDataRecord and ICustomTypeDescriptor, and provides data binding support for DbEnumerator.
OdbcDataReader Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited.
OleDbDataReader Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited.
OracleDataReader Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited.
SqlCeDataReader Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited.
SqlDataReader Provides a means of reading a forward-only stream of rows from a SQL Server database. This class cannot be inherited.

Remarks

The IDataReader and IDataRecord interfaces enable 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 the DataReader. For more information about implementing .NET Framework data providers, see Implementing a .NET Framework Data Provider.

An application does not create an instance of the IDataRecord interface directly, but creates an instance of a class that inherits IDataRecord. Typically this is done 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 alternate types, which conform to CLR types. As an example, the following table lists suggested mappings from OLE DB data types to CLR types, and lists alternate types in parentheses in the CLR Type column.

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

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Public Sub ReadMyData(myConnString As String)
    Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
    Dim myConnection As New SqlConnection(myConnString)
    Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
    myConnection.Open()
    Dim myReader As SqlDataReader
    myReader = myCommand.ExecuteReader()
    ' Always call Read before accessing data.
    While myReader.Read()
        Console.WriteLine((myReader.GetInt32(0) & ", " & myReader.GetString(1)))
    End While
    ' always call Close when done reading.
    myReader.Close()
    ' Close the connection when done with it.
    myConnection.Close()
End Sub 'ReadMyData

[C#] 
public void ReadMyData(string myConnString) {
    string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
    SqlConnection myConnection = new SqlConnection(myConnString);
    SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
    myConnection.Open();
    SqlDataReader myReader;
    myReader = myCommand.ExecuteReader();
    // Always call Read before accessing data.
    while (myReader.Read()) {
       Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
    }
    // always call Close when done reading.
    myReader.Close();
    // Close the connection when done with it.
    myConnection.Close();
 }

[C++] 
public:
void ReadMyData(String* myConnString) {
    String* mySelectQuery = S"SELECT OrderID, CustomerID FROM Orders";
    SqlConnection* myConnection = new SqlConnection(myConnString);
    SqlCommand* myCommand = new SqlCommand(mySelectQuery,myConnection);
    myConnection->Open();
    SqlDataReader* myReader;
    myReader = myCommand->ExecuteReader();
    // Always call Read before accessing data.
    while (myReader->Read()) {
       Console::WriteLine(S"{0}, {1}", __box(myReader->GetInt32(0)), myReader->GetString(1));
    }
    // always call Close when done reading.
    myReader->Close();
    // Close the connection when done with it.
    myConnection->Close();
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

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

See Also

IDataRecord Members | System.Data Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.