asp.net.ph

OleDbCommand.ExecuteReader Method ( )

System.Data.OleDb Namespace   OleDbCommand Class


Sends the CommandText to the Connection and builds an OleDbDataReader.

[ VB ]
Overloads Public Function ExecuteReader ( ) As OleDbDataReader

[ C# ]
public OleDbDataReader ExecuteReader ( );

[ C++ ]
public: OleDbDataReader* ExecuteReader ( );

[ JScript ]
public function ExecuteReader ( ) : OleDbDataReader;

Return Value

An OleDbDataReader object.

Exceptions


Exception Type Condition
InvalidOperationException Occurs when Cannot execute a command within a transaction context that differs from the context in which the connection was originally enlisted.

Remarks

When the CommandType property is set to StoredProcedure, the CommandText property should be set to the name of the stored procedure. The command executes this stored procedure when you call ExecuteReader.

While the OleDbDataReader is in use, the associated OleDbConnection is busy serving the OleDbDataReader. While in this state, no other operatons can be performed on the OleDbConnection other than closing it. This is the case until the Close method of the OleDbDataReader is called.

Example

The following example demonstrates using this version of the ExecuteReader method.

protected void ExecuteReaderDemo ( ) {
   // specify the data source
   OleDbConnection myConn = new OleDbConnection (
      "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + 
      Server.MapPath ( "~/app_data/dbtutor.mdb" ) );
   // define the command query
   String query = "SELECT ProductName, ProductDescription FROM Products";
   OleDbCommand myCommand = new OleDbCommand ( query, myConn );

   // open the connection and instantiate a datareader
   myConn.Open ( );
   OleDbDataReader myReader = myCommand.ExecuteReader ( );

   // display datareader contents into html table
   // first open the table and set up the table headers
   html += "<table cellspacing=1 class='data' width=90%>";
   html += "<tr>";
   html += "<th>Product Name</th>";
   html += "<th>Description</th>";
   html += "</tr>";

   // loop thru the reader
   while ( myReader.Read ( ) ) {
      html += "<tr>";
      html += "<td>" + myReader.GetString ( 0 ) + "</td>";
      html += "<td>" + myReader.GetString ( 1 ) + "</td>";
      html += "</tr>";
   }

   // close the table
   html += "</table>";

   // close the reader and the connection
   myReader.Close ( );
   myConn.Close ( );
}
</script>
  C# VB

 Show me 

See Also

OleDbCommand Members   OleDbCommand.ExecuteReader Overload List Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph