System.Data.SqlClient Namespace SqlCommand Class
Sets or retrieves the SqlConnection used by this instance of the SqlCommand.
Script |
SqlCommand.Connection [ = objConnection ] |
objConnection |
An object representing the connection to an SQL Server™ 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 myCommand = new SqlCommand ( query );
myCommand.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 myCommand As New SqlCommand ( query )
myCommand.Connection = New SqlConnection ( connString )
End Sub |
|
C# |
VB |
SqlCommand Members CommandText CommandTimeout CommandType