System.Data Namespace IDbCommand Class
Sets or retrieves the IDbConnection used by this instance of the IDbCommand.
Script |
IDbCommand.Connection [ = objConnection ] |
objConnection |
An object representing the connection to the 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. |
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 how the Connection with which to execute the command can be set.
void setSqlCommand ( ) {
string query = "SELECT * FROM Categories ORDER BY CategoryID";
string connString = "uid=sa; pwd=; database=northwind; server=myServer";
SqlCommand myCmd = new SqlCommand ( query );
myCmd.Connection = new SqlConnection ( connString );
}
Public Sub setSqlCommand ( )
Dim query As String = "SELECT * FROM Categories ORDER BY CategoryID"
Dim connString As String = "uid=sa; pwd=; database=northwind; server=myServer"
Dim myCmd As New SqlCommand ( query )
myCmd.Connection = New SqlConnection ( connString )
End Sub |
|
C# |
VB |
IDbCommand Members CommandText CommandTimeout CommandType