MySQL Connector/Net |
MySqlConnection..::.Open Method |
MySqlConnection Class Example See Also Send Feedback |
Opens a database connection with the property settings specified by the ConnectionString.
Namespace:
MySql.Data.MySqlClient
Assembly:
MySql.Data (in MySql.Data.dll) Version: 6.2.2.0
Syntax
C# |
---|
public override void Open() |
Visual Basic (Declaration) |
---|
Public Overrides Sub Open |
Visual C++ |
---|
public: virtual void Open() override |
Implements
IDbConnection..::.Open()()()Remarks
The MySqlConnection draws an open connection from the connection pool if one is available. Otherwise, it establishes a new connection to an instance of MySQL.
Examples
The following example creates a MySqlConnection, opens it,
displays some of its properties, then closes the connection.
CopyVB.NET
Public Sub CreateMySqlConnection(myConnString As String) Dim myConnection As New MySqlConnection(myConnString) myConnection.Open() MessageBox.Show("ServerVersion: " + myConnection.ServerVersion _ + ControlChars.Cr + "State: " + myConnection.State.ToString()) myConnection.Close() End Sub
CopyC#
public void CreateMySqlConnection(string myConnString) { MySqlConnection myConnection = new MySqlConnection(myConnString); myConnection.Open(); MessageBox.Show("ServerVersion: " + myConnection.ServerVersion + "\nState: " + myConnection.State.ToString()); myConnection.Close(); }
Exceptions
Exception | Condition |
---|---|
System..::.InvalidOperationException | Cannot open a connection without specifying a data source or server. |
MySql.Data.MySqlClient..::.MySqlException | A connection-level error occurred while opening the connection. |