System.Data Namespace IDbCommand Class
Sends the CommandText to the Connection and builds an IDataReader.
[ VB ]
Overloads Public Function ExecuteReader ( ) As IDataReader
[ C# ]
public IDataReader ExecuteReader ( );
[ C++ ]
public: IDataReader* ExecuteReader ( );
[ JScript ]
public function ExecuteReader ( ) : IDataReader;
An IDataReader object.
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.
The IDataReader supports a special mode that enables large binary values to be read efficiently. For more information, see the SequentialAccess setting for CommandBehavior.
While the IDataReader is in use, the associated Connection is busy serving the IDataReader. While in this state, no other operatons can be performed on the SqlConnection other than closing it. This is the case until the Close method of the IDataReader is called.
The following example demonstrates using this version of the ExecuteReader method.
protected void ExecuteReaderDemo ( ) {
// specify the data source
SqlConnection myConn = new SqlConnection (
"server=localhost; trusted_connection=yes; database=northwind" );
// define the command query
String query = "SELECT CustomerID, CompanyName FROM Customers";
SqlCommand myCmd = new SqlCommand ( query, myConn );
// open the connection and instantiate a datareader
myConn.Open ( );
IDataReader myReader = myCmd.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>Customer ID</th>";
html += "<th>Company Name</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>
protected sub ExecuteReaderDemo ( )
' specify the data source
dim myConn as new SqlConnection ( & _
"server=localhost; trusted_connection=yes; database=northwind" )
' define the command query
dim query as String = "SELECT CustomerID, CompanyName FROM Customers"
dim myCmd as new SqlCommand ( query, myConn )
' open the connection and instantiate a datareader
myConn.Open ( )
dim myReader as IDataReader = myCmd.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>Customer ID</th>"
html &= "<th>Company Name</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>"
end while
' close the table
html &= "</table>"
' close the reader and the connection
myReader.Close ( )
myConn.Close ( )
end sub
</script> |
|
C# |
VB |
Show me
IDbCommand Members IDbCommand.ExecuteReader Overload List