Connection Property

MySQL Connector/Net

Gets or sets the MySqlConnection used by this instance of the MySqlCommand.

Namespace:  MySql.Data.MySqlClient
Assembly:  MySql.Data (in MySql.Data.dll) Version: 6.2.2.0

Syntax

C#
public MySqlConnection Connection { get; set; }
Visual Basic (Declaration)
Public Property Connection As MySqlConnection
Visual C++
public:
property MySqlConnection^ Connection {
	MySqlConnection^ get ();
	void set (MySqlConnection^ value);
}

Field Value

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

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.

Examples

The following example creates a MySqlCommand and sets some of its properties.
CopyVB.NET
Public Sub CreateMySqlCommand()
Dim mySelectQuery As String = "SELECT * FROM mytable ORDER BY id"
Dim myConnectString As String = "Persist Security Info=False;database=test;server=myServer"
Dim myCommand As New MySqlCommand(mySelectQuery)
myCommand.Connection = New MySqlConnection(myConnectString)
myCommand.CommandType = CommandType.Text
End Sub
CopyC#
public void CreateMySqlCommand()
{
string mySelectQuery = "SELECT * FROM mytable ORDER BY id";
string myConnectString = "Persist Security Info=False;database=test;server=myServer";
MySqlCommand myCommand = new MySqlCommand(mySelectQuery);
myCommand.Connection = new MySqlConnection(myConnectString);
myCommand.CommandType = CommandType.Text;
}

See Also