Retrieving Data Using A DataReader
Retrieving Data Using A DataReader
To retrieve data using a DataReader, create an instance of the Command object, and then create a DataReader by calling
Command.ExecuteReader to retrieve rows from a data source. The DataReader provides an unbuffered stream of data that allows
procedural logic to efficiently process results from a data source sequentially. The DataReader is a good choice when you're retrieving
large amounts of data because the data is not cached in memory.
The following example illustrates using a DataReader, where reader represents a valid DataReader and command represents a valid
Command object.
C# = Copy
reader = command.ExecuteReader();
Use the DataReader.Read method to obtain a row from the query results. You can access each column of the returned row by passing
the name or ordinal number of the column to the DataReader. However, for best performance, the DataReader provides a series of
methods that allow you to access column values in their native data types (GetDateTime, GetDouble, GetGuid, GetInt32, and so on).
For a list of typed accessor methods for data provider-specific DataReaders, see OleDbDataReader and SqlDataReader. Using the
typed accessor methods when you know the underlying data type reduces the amount of type conversion required when retrieving
the column value.
The following example iterates through a DataReader object and returns two columns from each row.
C# = Copy
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
reader.GetString(1));
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
}
Closing the DataReader
Always call the Close method when you have finished using the DataReader object.
If your Command contains output parameters or return values, those values are not available until the DataReader is closed.
While a DataReader is open, the Connection is in use exclusively by that DataReader. You cannot execute any commands for the
Connection, including creating another DataReader, until the original DataReader is closed.
7 Note
Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class.
In a finalizer, only release unmanaged resources that your class owns directly. If your class does not own any unmanaged
resources, do not include a Finalize method in your class definition. For more information, see Garbage Collection.
C# = Copy
while (reader.HasRows)
{
Console.WriteLine("\t{0}\t{1}", reader.GetName(0),
reader.GetName(1));
while (reader.Read())
{
Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0),
reader.GetString(1));
}
reader.NextResult();
}
}
}
C# = Copy
The ADO.NET DataSet can also be used to represent hierarchical rowsets by using parent-child relationships between tables. For more
information, see DataSets, DataTables, and DataViews.
The following code example uses the MSDataShape Provider to generate a chapter column of orders for each customer in a list of
customers.
C# = Copy
using (OleDbConnection connection = new OleDbConnection(
"Provider=MSDataShape;Data Provider=SQLOLEDB;" +
"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind"))
{
using (OleDbCommand custCMD = new OleDbCommand(
"SHAPE {SELECT CustomerID, CompanyName FROM Customers} " +
"APPEND ({SELECT CustomerID, OrderID FROM Orders} AS CustomerOrders " +
"RELATE CustomerID TO CustomerID)", connection))
{
connection.Open();
while (custReader.Read())
{
Console.WriteLine("Orders for " + custReader.GetString(1));
// custReader.GetString(1) = CompanyName
while (orderReader.Read())
Console.WriteLine("\t" + orderReader.GetInt32(1));
// orderReader.GetInt32(1) = OrderID
orderReader.Close();
}
}
// Make sure to always close readers and connections.
custReader.Close();
}
}
}
Returning results with Oracle REF CURSORs
The .NET Framework Data Provider for Oracle supports the use of Oracle REF CURSORs to return a query result. An Oracle REF
CURSOR is returned as an OracleDataReader.
You can retrieve an OracleDataReader object that represents an Oracle REF CURSOR by using the ExecuteReader method. You can also
specify an OracleCommand that returns one or more Oracle REF CURSORs as the SelectCommand for an OracleDataAdapter used to
fill a DataSet.
To access a REF CURSOR returned from an Oracle data source, create an OracleCommand for your query and add an output parameter
that references the REF CURSOR to the Parameters collection of your OracleCommand. The name of the parameter must match the
name of the REF CURSOR parameter in your query. Set the type of the parameter to OracleType.Cursor. The
OracleCommand.ExecuteReader() method of your OracleCommand returns an OracleDataReader for the REF CURSOR.
If your OracleCommand returns multiple REF CURSORS, add multiple output parameters. You can access the different REF CURSORs by
calling the OracleCommand.ExecuteReader() method. The call to ExecuteReader() returns an OracleDataReader referencing the first REF
CURSOR. You can then call the OracleDataReader.NextResult() method to access subsequent REF CURSORs. Although the parameters
in your OracleCommand.Parameters collection match the REF CURSOR output parameters by name, the OracleDataReader accesses
them in the order in which they were added to the Parameters collection.
For example, consider the following Oracle package and package body.
SQL = Copy
The following code creates an OracleCommand that returns the REF CURSORs from the previous Oracle package by adding two
parameters of type OracleType.Cursor to the OracleCommand.Parameters collection.
C# = Copy
The following code returns the results of the previous command using the Read() and NextResult() methods of the OracleDataReader.
The REF CURSOR parameters are returned in order.
C# = Copy
oraConn.Open();
Console.WriteLine("\nEmp ID\tName");
while (reader.Read())
Console.WriteLine("{0}\t{1}, {2}", reader.GetOracleNumber(0), reader.GetString(1), reader.GetString(2));
reader.NextResult();
Console.WriteLine("\nDept ID\tName");
while (reader.Read())
Console.WriteLine("{0}\t{1}", reader.GetOracleNumber(0), reader.GetString(1));
// Make sure to always close readers and connections.
reader.Close();
oraConn.Close();
The following example uses the previous command to populate a DataSet with the results of the Oracle package.
C# = Copy
adapter.Fill(ds);
7 Note
To avoid an OverflowException, we recommend that you also handle any conversion from the Oracle NUMBER type to a valid
.NET Framework type before storing the value in a DataRow. You can use the FillError event to determine if an
OverflowException has occurred. For more information on the FillError event, see Handling DataAdapter Events.
See also
DataAdapters and DataReaders
Commands and Parameters
Retrieving Database Schema Information
ADO.NET Overview