IDataRecord Interface
Assembly: System.Data (in system.data.dll)
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 the DataReader. For more information about how to implement .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, 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 |
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.
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()) { Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1])); } // Call Close when done reading. reader.Close(); } }
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.