.NET Framework Class Library  

OdbcCommand.Connection Property

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Gets or sets the OdbcConnection used by this instance of the OdbcCommand.

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

Property Value

The connection to a data source. The default is a null value.

Exceptions

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

Remarks

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 you set Connection after the transaction has been committed or rolled back, and the Transaction property is not null, the Transaction property is then set to a null value.

Example

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

[Visual Basic] 
Public Sub CreateMyOdbcCommand()
    Dim mySelectQuery As String = _
       "SELECT * FROM Categories ORDER BY CategoryID"
    Dim myCommand As New OdbcCommand(mySelectQuery)
    myCommand.Connection = New OdbcConnection _
       ("DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;")
    myCommand.CommandTimeout = 20
End Sub

[C#] 
public void CreateMyOdbcCommand() 
{
   string mySelectQuery = "SELECT * FROM Categories ORDER BY CategoryID";
   OdbcCommand myCommand = new OdbcCommand(mySelectQuery);
   myCommand.Connection = new OdbcConnection
      ("DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;");
   myCommand.CommandTimeout = 20;
}

[C++] 
public:
    void CreateMyOdbcCommand()
    {
        String* mySelectQuery = S"SELECT * FROM Categories ORDER BY CategoryID";
        OdbcCommand* myCommand = new OdbcCommand(mySelectQuery);
        myCommand->Connection = new OdbcConnection
            (S"DRIVER= {SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;");
        myCommand->CommandTimeout = 20;
    };

[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

See Also

OdbcCommand Class | OdbcCommand Members | System.Data.Odbc Namespace | CommandText | CommandTimeout | CommandType