Export (0) Print
Expand All

SqlCommand.Connection Property

Gets or sets the SqlConnection used by this instance of the SqlCommand.

[Visual Basic]
Public Property Connection As SqlConnection
[C#]
public SqlConnection Connection {get; set;}
[C++]
public: __property SqlConnection* get_Connection();
public: __property void set_Connection(SqlConnection*);
[JScript]
public function get Connection() : SqlConnection;
public function set Connection(SqlConnection);

Property Value

The connection to a data source. The default value is a null reference (Nothing in Visual Basic).

Exceptions

Exception Type Condition
InvalidOperationException The Connection property was changed while a transaction was in progress.

Remarks

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.

Example

[Visual Basic, C#, C++] The following example creates a SqlCommand and sets some of its properties.

[Visual Basic] 
Public Sub CreateMySqlCommand()
    Dim mySelectQuery As String = "SELECT * FROM Categories ORDER BY CategoryID"
    Dim myConnectString As String = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"
    Dim myCommand As New SqlCommand(mySelectQuery)
    myCommand.Connection = New SqlConnection(myConnectString)
    myCommand.CommandTimeout = 15
    myCommand.CommandType = CommandType.Text
End Sub 'CreateMySqlCommand

[C#] 
public void CreateMySqlCommand() 
 {
    string mySelectQuery = "SELECT * FROM Categories ORDER BY CategoryID";
    string myConnectString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
    SqlCommand myCommand = new SqlCommand(mySelectQuery);
    myCommand.Connection = new SqlConnection(myConnectString);
    myCommand.CommandTimeout = 15;
    myCommand.CommandType = CommandType.Text;
 }

[C++] 
public:
void CreateMySqlCommand() 
 {
    String* mySelectQuery = S"SELECT * FROM Categories ORDER BY CategoryID";
    String* myConnectString = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
    SqlCommand* myCommand = new SqlCommand(mySelectQuery);
    myCommand->Connection = new SqlConnection(myConnectString);
    myCommand->CommandTimeout = 15;
    myCommand->CommandType = CommandType::Text;
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

SqlCommand Class | SqlCommand Members | System.Data.SqlClient Namespace | CommandText | CommandTimeout | CommandType

Show:
© 2014 Microsoft