System.Data.Common.DbDataReader
is returned by the ExecuteReader()
method of System.Data.Common.DbCommand
. DbDataReader
is used if the DbCommand
represents an SQL statement that returns values, typically a select
statements. The returned DbDataReader
implements System.Collection.IEnumerable
, so the returned records from the SQL statement can be iterated over with a foreach
… in
statement. DbDataReader
type is GetSchemaTable()
: it returns the metadata about the columns of the queries resultset, notably column names and their data types. DbDataReader
is an abstract class, so these (or at least some of these) methods and properties must be overwritten in derived classes. Close() | |
CloseAsync() | |
Depth | |
Dispose() | |
DisposeAsync() | |
FieldCount | Number of columns in the result set. |
GetTYPE(pos) | Returns the value of pos as TYPE . TYPE is for example Byte , Char , DataTime , Decimal , Double , Int16 , Int32 , Int64 , String etc. |
GetBytes() | |
GetChars() | |
GetColumnSchemaAsync() | |
GetData() | Returns the nested data reader (also a DbDataReader object). Compare with GetDbDataReader() |
GetDataTypeName(pos) | Returns the type of pos as string. Compare with GetFieldType() |
GetDbDataReader() | Returns a DbDataReader . Compare with GetData() |
GetEnumerator() | Returns a System.Collections.IEnumerator . |
GetFieldType(pos) | Returns a System.Type that corresponds to the type of pos . Compare with GetDataTypeName() |
GetFieldValue<T>(pos) | Returns the value of pos as type T |
GetFieldValueAsync() | |
GetName(pos) | Name of column pos |
GetOrdinal(name) | Gets the column number that corresponds to the column whose name is name . |
GetProviderSpecificFieldType() | |
GetProviderSpecificValue() | |
GetProviderSpecificValues() | |
GetSchemaTable() | |
GetSchemaTableAsync() | |
GetStream() | Returns a System.IO.Stream |
GetTextReader() | Returns a System.IO.TextReader |
GetValue(pos) | Gets the value of pos as a System.Object |
GetValues() | |
HasRows | |
IsClosed | |
IsDBNull(pos) | true if the corresponding value is an (SQL) null value. |
IsDBNullAsync() | |
Item[] | |
NextResult() | |
NextResultAsync() | |
Read() | |
ReadAsync() | |
RecordsAffected | |
VisibleFieldCount |
DbDataReader
instance can be used to iterate over the result set of an SQL statement. using System; using System.Data.Common; using System.Data.OleDb; using System.IO; class Prg { static void Main () { string csvFilePath = Directory.GetCurrentDirectory(); string provider = // "Microsoft.Jet.OLEDB.4.0" "Microsoft.ACE.OLEDB.12.0"; string connectionString = $"Provider={provider};" + $"Data Source={csvFilePath};" + $"Extended Properties='text';"; using OleDbConnection connection = new OleDbConnection(connectionString); connection.Open(); DbCommand command = new OleDbCommand("select id, num, txt from data.csv", connection); // // Of course, the constructor returns an OleDbCommand object. Thus, the // following line would probably be more correct. But because this // program serves at demonstrating the basic functionality of ExecuteReader() and // Read(), I have not done so. // // OleDbCommand command = new OleDbCommand("select id, num, txt from data.csv", connection); DbDataReader reader = command.ExecuteReader(); // // With an OleDbCommand, the ExecuteReader method returns // a DbDataReader object: // // OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine( String.Format(" {0,2} | {1,5:F1} | {2}", reader.GetInt32 (0), reader.GetDouble(1), reader.GetString(2) )); } } }
id,num,txt 1,5,five 2,99.9,ninty-nine dot nine 3,-42,minus forty-two