MySQL Connector/Net
MySqlConnection Class |
Represents an open connection to a MySQL Server database. This class cannot be inherited.

SystemObject SystemMarshalByRefObject
System.ComponentModelComponent
System.Data.CommonDbConnection
MySql.Data.MySqlClientMySqlConnection
System.ComponentModelComponent
System.Data.CommonDbConnection
MySql.Data.MySqlClientMySqlConnection
Namespace: MySql.Data.MySqlClient
Assembly: MySql.Data (in MySql.Data.dll) Version: 6.9.9

public sealed class MySqlConnection : DbConnection, IDisposable, ICloneable
Public NotInheritable Class MySqlConnection Inherits DbConnection Implements IDisposable, ICloneable
public ref class MySqlConnection sealed : public DbConnection, IDisposable, ICloneable
[<SealedAttribute>] type MySqlConnection = class inherit DbConnection interface IDisposable interface ICloneable end
The MySqlConnection type exposes the following members.

Name | Description | |
---|---|---|
![]() ![]() | MySqlConnection |
Initializes a new instance of the MySqlConnection class.
|
![]() ![]() | MySqlConnection(String) |
Initializes a new instance of the MySqlConnection class when given a string containing the connection string.
|

Name | Description | |
---|---|---|
![]() | CanRaiseEvents | Gets a value indicating whether the component can raise an event. (Inherited from Component.) |
![]() ![]() | ConnectionString |
Gets or sets the string used to connect to a MySQL Server database.
(Overrides DbConnectionConnectionString.) |
![]() ![]() | ConnectionTimeout |
Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.
(Overrides DbConnectionConnectionTimeout.) |
![]() | Container | Gets the IContainer that contains the Component. (Inherited from Component.) |
![]() ![]() | Database | Gets the name of the current database or the database to be used after a connection is opened. (Overrides DbConnectionDatabase.) |
![]() | DataSource |
Gets the name of the MySQL server to which to connect.
(Overrides DbConnectionDataSource.) |
![]() | DbProviderFactory | (Overrides DbConnectionDbProviderFactory.) |
![]() | DesignMode | Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component.) |
![]() | Events | Gets the list of event handlers that are attached to this Component. (Inherited from Component.) |
![]() | IsPasswordExpired | |
![]() | ServerThread |
Returns the id of the server thread this connection is executing on
|
![]() ![]() | ServerVersion | Gets a string containing the version of the MySQL server to which the client is connected. (Overrides DbConnectionServerVersion.) |
![]() | Site | (Inherited from Component.) |
![]() ![]() | State | Gets the current state of the connection. (Overrides DbConnectionState.) |
![]() | UseCompression |
Indicates if this connection should use compression when communicating with the server.
|

Name | Description | |
---|---|---|
![]() | BeginDbTransaction | (Overrides DbConnectionBeginDbTransaction(IsolationLevel).) |
![]() ![]() | BeginTransaction | Begins a database transaction. |
![]() ![]() | BeginTransaction(IsolationLevel) | Begins a database transaction with the specified isolation level. |
![]() | BeginTransactionAsync |
Async version of BeginTransaction
|
![]() | BeginTransactionAsync(IsolationLevel) |
Async version of BeginTransaction
|
![]() | BeginTransactionAsync(CancellationToken) | |
![]() | BeginTransactionAsync(IsolationLevel, CancellationToken) | |
![]() | CancelQuery | |
![]() ![]() | ChangeDatabase | Changes the current database for an open MySqlConnection. (Overrides DbConnectionChangeDatabase(String).) |
![]() | ChangeDataBaseAsync(String) | |
![]() | ChangeDataBaseAsync(String, CancellationToken) |
Async version of ChangeDataBase
|
![]() ![]() | ClearAllPools | Clears all connection pools. |
![]() | ClearAllPoolsAsync |
Async version of ClearAllPools
|
![]() | ClearAllPoolsAsync(CancellationToken) | |
![]() ![]() | ClearPool | Empties the connection pool associated with the specified connection. |
![]() | ClearPoolAsync(MySqlConnection) |
Async version of ClearPool
|
![]() | ClearPoolAsync(MySqlConnection, CancellationToken) | |
![]() | Clone |
Creates a new MySqlConnection object with the exact same ConnectionString value
|
![]() ![]() | Close | Closes the connection to the database. This is the preferred method of closing any open connection. (Overrides DbConnectionClose.) |
![]() | CloseAsync |
Async version of Close
|
![]() | CloseAsync(CancellationToken) | |
![]() | CreateCommand |
Creates and returns a MySqlCommand object associated with the MySqlConnection.
|
![]() | CreateDbCommand | (Overrides DbConnectionCreateDbCommand.) |
![]() | CreateObjRef | Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.) |
![]() | Dispose | |
![]() | Dispose(Boolean) | (Overrides ComponentDispose(Boolean).) |
![]() | EnlistTransaction |
Enlists in the specified transaction.
(Overrides DbConnectionEnlistTransaction(Transaction).) |
![]() | Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) |
![]() | Finalize | (Overrides ComponentFinalize.) |
![]() | GetHashCode | Serves as the default hash function. (Inherited from Object.) |
![]() | GetLifetimeService | Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.) |
![]() | GetSchema |
Returns schema information for the data source of this DbConnection.
(Overrides DbConnectionGetSchema.) |
![]() | GetSchema(String) |
Returns schema information for the data source of this
DbConnection using the specified string for the schema name.
(Overrides DbConnectionGetSchema(String).) |
![]() | GetSchema(String, String) |
Returns schema information for the data source of this DbConnection
using the specified string for the schema name and the specified string array
for the restriction values.
(Overrides DbConnectionGetSchema(String, String).) |
![]() | GetSchemaCollection | |
![]() | GetSchemaCollectionAsync(String, String) |
Async version of GetSchemaCollection
|
![]() | GetSchemaCollectionAsync(String, String, CancellationToken) | |
![]() | GetService | (Inherited from Component.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | InitializeLifetimeService | Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | MemberwiseClone(Boolean) | Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.) |
![]() | OnStateChange | (Inherited from DbConnection.) |
![]() ![]() | Open | Opens a database connection with the property settings specified by the ConnectionString. (Overrides DbConnectionOpen.) |
![]() | OpenAsync | (Inherited from DbConnection.) |
![]() | OpenAsync(CancellationToken) | (Inherited from DbConnection.) |
![]() | Ping |
Ping
|
![]() | ToString | Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.) |

Name | Description | |
---|---|---|
![]() | Disposed | Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.) |
![]() | InfoMessage | Occurs when MySQL returns warnings as a result of executing a command or query. |
![]() | StateChange | (Inherited from DbConnection.) |

A MySqlConnection object represents a session to a MySQL Server data source. When you create an instance of MySqlConnection, all properties are set to their initial values. For a list of these values, see the MySqlConnection constructor.
If the MySqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close or Dispose.

The following example creates a MySqlCommand and
a MySqlConnection. The MySqlConnection is opened and set as the
Connection for the MySqlCommand. The example then calls
ExecuteNonQuery, and closes the connection. To accomplish this, the ExecuteNonQuery is
passed a connection string and a query string that is a SQL INSERT
statement.
<c> public void InsertRow(string myConnectionString) { // If the connection string is null, use a default. if(myConnectionString == "") { myConnectionString = "Database=Test;Data Source=localhost;User Id=username;Password=pass"; } MySqlConnection myConnection = new MySqlConnection(myConnectionString); string myInsertQuery = "INSERT INTO Orders (id, customerId, amount) Values(1001, 23, 30.66)"; MySqlCommand myCommand = new MySqlCommand(myInsertQuery); myCommand.Connection = myConnection; myConnection.Open(); myCommand.ExecuteNonQuery(); myCommand.Connection.Close(); } </c>
<c> Public Sub InsertRow(myConnectionString As String) ' If the connection string is null, use a default. If myConnectionString = "" Then myConnectionString = "Database=Test;Data Source=localhost;User Id=username;Password=pass" End If Dim myConnection As New MySqlConnection(myConnectionString) Dim myInsertQuery As String = "INSERT INTO Orders (id, customerId, amount) Values(1001, 23, 30.66)" Dim myCommand As New MySqlCommand(myInsertQuery) myCommand.Connection = myConnection myConnection.Open() myCommand.ExecuteNonQuery() myCommand.Connection.Close() End Sub </c>
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
