Connection Property

MySQL Connector.Net

Collapse image Expand Image Copy image CopyHover image
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.8.4.0

Syntax

C#
public MySqlConnection Connection { get; set; }
Visual Basic
Public Property Connection As MySqlConnection
	Get
	Set
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.
Visual Basic Copy imageCopy
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
C# Copy imageCopy
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