The DataReader object represents the results of a database query. You get a DataReader by calling a Command object’s ExecuteReader() method.
Whether a DataReader represents any rows by checking the HasRows property or calling the Read() method. The Read() method returns true when the DataReader can advance to a new row.
The DataReader represents a single row of data at a time. To get the next row of data, you need to call the Read() method. When you get to the last row, the Read() method returns False.
There are multiple ways to refer to the columns returned by a DataReader.
If you want to retrieve the value of the Title column for the current row represented by a DataReader, then you can use any of the following methods:
string title = (string)reader[“<FieldName>”];
string title = (string)reader[0];
string title = reader.GetString(0);
SqlString title = reader.GetSqlString(0);
The first method returns the Title column by name. The value of the Title column is returned as an Object. Therefore, you must cast the value to a string before you can assign the value to a string variable.
The second method returns the Title column by position. It also returns the value of the Title column as an Object, so you must cast the value before using it.
The third method returns the Title column by position. However, it retrieves the value as a String value. You don’t need to cast the value in this case.
The fourth method returns the Title column by position. However, it returns the value as a SqlString rather than a normal String. A SqlString represents the value using the specialized data types defined in the System.Data.SqlTypes namespace.
Whether a DataReader represents any rows by checking the HasRows property or calling the Read() method. The Read() method returns true when the DataReader can advance to a new row.
The DataReader represents a single row of data at a time. To get the next row of data, you need to call the Read() method. When you get to the last row, the Read() method returns False.
There are multiple ways to refer to the columns returned by a DataReader.
If you want to retrieve the value of the Title column for the current row represented by a DataReader, then you can use any of the following methods:
string title = (string)reader[“<FieldName>”];
string title = (string)reader[0];
string title = reader.GetString(0);
SqlString title = reader.GetSqlString(0);
The first method returns the Title column by name. The value of the Title column is returned as an Object. Therefore, you must cast the value to a string before you can assign the value to a string variable.
The second method returns the Title column by position. It also returns the value of the Title column as an Object, so you must cast the value before using it.
The third method returns the Title column by position. However, it retrieves the value as a String value. You don’t need to cast the value in this case.
The fourth method returns the Title column by position. However, it returns the value as a SqlString rather than a normal String. A SqlString represents the value using the specialized data types defined in the System.Data.SqlTypes namespace.
The ADO.NET Framework encompasses a huge number of classes. It really consists of the following three classes:
The importance of these three classes, it’s safe to tell you that they don’t really exist. ADO.NET uses the Provider model. You use different sets of ADO.NET classes for communicating with different data sources.
The different implementations of the Connection, Command, and DataReader classes are grouped into the following namespaces:
If you are communicating with an Oracle database, you should use the classes from the OracleClient namespace. If you are communicating with another type of database, you need to use the classes from either the OleDb or Odbc namespaces. Just about every database ever created has either an OLEDB provider or an ODBC driver.
Because ADO.NET follows the Provider model, all implementations of the Connection, Command, and DataReader classes inherit from a set of base classes. Here is a list of these base classes:
- Connection : Enables you to represent a connection to a data source.
- Command : Enables you to execute a command against a data source.
- DataReader : Enables you to represent data retrieved from a data source.
The importance of these three classes, it’s safe to tell you that they don’t really exist. ADO.NET uses the Provider model. You use different sets of ADO.NET classes for communicating with different data sources.
The different implementations of the Connection, Command, and DataReader classes are grouped into the following namespaces:
- System.Data.SqlClient : Contains ADO.NET classes for connecting to Microsoft SQL Server version 7.0 or higher.
- System.Data.OleDb : Contains ADO.NET classes for connecting to a data source with an OLEDB provider.
- System.Data.Odbc : Contains ADO.NET classes for connecting to a data source with an ODBC driver.
- System.Data.OracleClient : Contains ADO.NET classes for connecting to an Oracle database (requires Oracle 8i Release 3/8.1.7 Client or later).
- System.Data.SqlServerCe : Contains ADO.NET classes for connecting to SQL Server Mobile.
If you are communicating with an Oracle database, you should use the classes from the OracleClient namespace. If you are communicating with another type of database, you need to use the classes from either the OleDb or Odbc namespaces. Just about every database ever created has either an OLEDB provider or an ODBC driver.
Because ADO.NET follows the Provider model, all implementations of the Connection, Command, and DataReader classes inherit from a set of base classes. Here is a list of these base classes:
- DbConnection : The base class for all Connection classes.
- DbCommand : The base class for all Command classes.
- DbDataReader : The base class for all DataReader classes.
DataReader:
The DataReader loads one record from the data store at a time. Each time the DataReader's Read() method is called, the DataReader discards the current record, goes back to the database, and fetches the next record in the resultset. The Read() method returns True if a row was loaded from the database, and False if there are no more rows.
DataSet:
DataSets are a more complex and feature-rich object than DataReaders. Whereas DataReaders simply scuttle data back from a data store, DataSets can be thought of as in-memory databases. Just like a database is comprised of a set of tables, a DataSet is made up of a collection of DataTable objects. Whereas a database can have relationships among its tables, along with various data integrity constraints on the fields of the tables, so too can a DataSet have relationships among its DataTables and constraints on its DataTables' fields
The DataReader loads one record from the data store at a time. Each time the DataReader's Read() method is called, the DataReader discards the current record, goes back to the database, and fetches the next record in the resultset. The Read() method returns True if a row was loaded from the database, and False if there are no more rows.
DataSet:
DataSets are a more complex and feature-rich object than DataReaders. Whereas DataReaders simply scuttle data back from a data store, DataSets can be thought of as in-memory databases. Just like a database is comprised of a set of tables, a DataSet is made up of a collection of DataTable objects. Whereas a database can have relationships among its tables, along with various data integrity constraints on the fields of the tables, so too can a DataSet have relationships among its DataTables and constraints on its DataTables' fields