System.Data.OleDb Namespace OleDbCommand Class
Sets or retrieves the OleDbConnection used by this instance of the OleDbCommand.
Script |
OleDbCommand.Connection [ = objConnection ] |
objConnection |
An object representing the connection to an OLEDB data source. |
The property is read/write with no default value.
Exception Type |
Condition |
InvalidOperationException |
Occurs when the Connection property is changed while a transaction is in progress. |
You cannot set the Connection, CommandType and CommandText properties if the current connection is performing an execute or fetch operation.
If you set Connection while a transaction is in progress and the Transaction property is not null, an InvalidOperationException is generated. If the Transaction property is not null and the transaction has already been committed or rolled back, Transaction is set to null.
The following example illustrates using the Connection property.
void ConnectionDemo ( ) {
string query = "SELECT * FROM Categories ORDER BY CategoryID";
OleDbCommand myCommand = new OleDbCommand ( query );
myCommand.Connection = new OleDbConnection
( "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=Northwind.mdb" );
myCommand.CommandTimeout = 20;
}
Public Sub ConnectionDemo ( )
Dim query As String = "SELECT * FROM Categories ORDER BY CategoryID"
Dim myCommand As New OleDbCommand ( query )
myCommand.Connection = New OleDbConnection _
( "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=Northwind.mdb" )
myCommand.CommandTimeout = 20
End Sub |
|
C# |
VB |
OleDbCommand Members CommandText CommandTimeout CommandType