DB2 10.5 for Linux, UNIX, and Windows

DB2Command.Connection Property

Gets or sets the DB2Connection used by this instance of the DB2Command.

Namespace:
IBM.Data.DB2
Assembly:
IBM.Data.DB2 (in IBM.Data.DB2.dll)

Syntax

[Visual Basic]
Public Property Connection As DB2Connection

[C#]
public DB2Connection
 Connection {get; set;}
[C++]
public: __property DB2Connection
* get_Connection();
public: __property void set_Connection(DB2Connection
*);
[JScript]
public function get Connection() : DB2Connection
;
public function set Connection(DB2Connection
);

Property value

The connection to a database. 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#] The following example creates a DB2®Command and sets some of its properties.

[Visual Basic]
Public Sub CreateMyDB2Command()
    Dim mySelectQuery As String = _
       "SELECT * FROM EMPLOYEE ORDER BY EMPNO"
    Dim myCommand As New DB2Command(mySelectQuery)
    myCommand.Connection = New DB2Connection _
       ("DATABASE=SAMPLE;")
End Sub

[C#]
public void CreateMyDB2Command()
{
   string mySelectQuery = "SELECT * FROM EMPLOYEE ORDER BY EMPNO";
   DB2Command myCommand = new DB2Command(mySelectQuery);
   myCommand.Connection = new DB2Connection
      ("DATABASE=SAMPLE;");
}