System.Data.OleDb Namespace
Represents an SQL statement or stored procedure to execute at a data source.
When an instance of OleDbCommand is created, the read/write properties are set to their initial values. For a list of these values, see the OleDbCommand constructor.
OleDbCommand features the following methods to execute commands at a data source:
Item |
Description |
ExecuteReader |
Executes commands that return rows. ExecuteReader may not have the desired effect if used to execute commands such as SQL SET statements. |
ExecuteNonQuery |
Executes commands such as SQL INSERT, DELELE, UPDATE, AND SET statements. |
ExecuteScalar |
Retrieves a single value ( for example, an aggregate value ) from a database. |
If a fatal OleDbException ( for example, an SQL Server™ severity level of 20 or greater ) is generated by the method executing an OleDbCommand, the OleDbConnection, the connection may be closed. However, the user can reopen the connection and continue.
The following example uses the OleDbCommand, along OleDbDataAdapter and OleDbConnection, to select rows from an Access database. The filled DataSet is then returned. The example is passed an initialized DataSet, a connection string, a query string that is an SQL SELECT statement, and a string that is the name of the source database table.
void ReadMyData ( string connString ) {
string query = "SELECT OrderID, CustomerID FROM Orders";
OleDbConnection myConn = new OleDbConnection ( connString );
OleDbCommand myCommand = new OleDbCommand ( query, myConn );
myConn.Open ( );
OleDbDataReader myReader = myCommand.ExecuteReader ( );
try {
while ( myReader.Read ( ) ) {
Response.Write ( myReader.GetInt32 ( 0 ) + ", " + myReader.GetString ( 1 ) );
}
}
finally {
myReader.Close ( );
myConn.Close ( );
}
}
Public Sub ReadMyData ( connString As String )
Dim query As String = "SELECT OrderID, CustomerID FROM Orders"
Dim myConn As New OleDbConnection ( connString )
Dim myCommand As new OleDbCommand ( query, myConn )
myConn.Open ( )
Dim myReader As OleDbDataReader = myCommand.ExecuteReader ( )
Try
While myReader.Read ( )
Response.Write ( myReader.GetInt32 ( 0 ).ToString ( ) & ", " & myReader.GetString ( 1 ) )
End While
Finally
myReader.Close ( )
myConn.Close ( )
End Try
End Sub |
|
C# |
VB |
OleDbDataAdapter OleDbConnection