Click or drag to resize
MySqlConnection Class
Represents an open connection to a MySQL Server database. This class cannot be inherited.
Inheritance Hierarchy

Namespace:  MySql.Data.MySqlClient
Assembly:  MySql.Data (in MySql.Data.dll) Version: 6.10.9
Syntax
public sealed class MySqlConnection : DbConnection, 
	ICloneable

The MySqlConnection type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleMySqlConnection
Initializes a new instance of the MySqlConnection class.
Public methodCode exampleMySqlConnection(String)
Initializes a new instance of the MySqlConnection class when given a string containing the connection string.
Top
Properties
  NameDescription
Protected propertyCanRaiseEvents
Gets a value indicating whether the component can raise an event.
(Inherited from Component.)
Public propertyCode exampleConnectionString
Gets or sets the string used to connect to a MySQL Server database.
(Overrides DbConnectionConnectionString.)
Public propertyCode exampleConnectionTimeout
Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.
(Overrides DbConnectionConnectionTimeout.)
Public propertyContainer
Gets the IContainer that contains the Component.
(Inherited from Component.)
Public propertyCode exampleDatabase
Gets the name of the current database or the database to be used after a connection is opened.
(Overrides DbConnectionDatabase.)
Public propertyDataSource
Gets the name of the MySQL server to which to connect.
(Overrides DbConnectionDataSource.)
Protected propertyDbProviderFactory (Overrides DbConnectionDbProviderFactory.)
Protected propertyDesignMode
Gets a value that indicates whether the Component is currently in design mode.
(Inherited from Component.)
Protected propertyEvents
Gets the list of event handlers that are attached to this Component.
(Inherited from Component.)
Public propertyIsPasswordExpired
Gets a boolean value that indicates whether the password associated to the connection is expired.
Public propertyServerThread
Returns the id of the server thread this connection is executing on
Public propertyCode exampleServerVersion
Gets a string containing the version of the MySQL server to which the client is connected.
(Overrides DbConnectionServerVersion.)
Public propertySite
Gets or sets the ISite of the Component.
(Inherited from Component.)
Public propertyCode exampleState
Gets the current state of the connection.
(Overrides DbConnectionState.)
Public propertyUseCompression
Indicates if this connection should use compression when communicating with the server.
Top
Methods
  NameDescription
Protected methodBeginDbTransaction (Overrides DbConnectionBeginDbTransaction(IsolationLevel).)
Public methodCode exampleBeginTransaction
Begins a database transaction.
Public methodCode exampleBeginTransaction(IsolationLevel)
Begins a database transaction with the specified isolation level.
Public methodBeginTransactionAsync
Initiates the asynchronous execution of a transaction.
Public methodBeginTransactionAsync(IsolationLevel)
Asynchronous version of BeginTransaction.
Public methodBeginTransactionAsync(CancellationToken)
Asynchronous version of BeginTransaction.
Public methodBeginTransactionAsync(IsolationLevel, CancellationToken)
Asynchronous version of BeginTransaction.
Public methodCancelQuery
Cancels the query after the specified time interval.
Public methodCode exampleChangeDatabase
Changes the current database for an open MySqlConnection.
(Overrides DbConnectionChangeDatabase(String).)
Public methodChangeDataBaseAsync(String)
Asynchronous version of the ChangeDataBase method.
Public methodChangeDataBaseAsync(String, CancellationToken)
Asynchronous version of the ChangeDataBase method.
Public methodStatic memberClearAllPools
Clears all connection pools.
Public methodClearAllPoolsAsync
Asynchronous version of the ClearAllPools method.
Public methodClearAllPoolsAsync(CancellationToken)
Asynchronous version of the ClearAllPools method.
Public methodStatic memberClearPool
Empties the connection pool associated with the specified connection.
Public methodClearPoolAsync(MySqlConnection)
Asynchronous version of the ClearPool method.
Public methodClearPoolAsync(MySqlConnection, CancellationToken)
Asynchronous version of the ClearPool method.
Public methodClone
Creates a new MySqlConnection object with the exact same ConnectionString value
Public methodCode exampleClose
Closes the connection to the database. This is the preferred method of closing any open connection.
(Overrides DbConnectionClose.)
Public methodCloseAsync
Asynchronous version of the Close method.
Public methodCloseAsync(CancellationToken)
Asynchronous version of the Close method.
Public methodCreateCommand
Creates and returns a MySqlCommand object associated with the MySqlConnection.
Protected methodCreateDbCommand (Overrides DbConnectionCreateDbCommand.)
Public methodCreateObjRef
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
(Inherited from MarshalByRefObject.)
Public methodDispose
Releases all resources used by the MySqlConnection
Protected methodDispose(Boolean)
Releases the unmanaged resources used by the MySqlConnection and optionally releases the managed resources
(Overrides ComponentDispose(Boolean).)
Public methodEnlistTransaction
Enlists in the specified transaction.
(Overrides DbConnectionEnlistTransaction(Transaction).)
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize (Overrides ComponentFinalize.)
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetLifetimeService
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject.)
Public methodGetSchema
Returns schema information for the data source of this DbConnection.
(Overrides DbConnectionGetSchema.)
Public methodGetSchema(String)
Returns schema information for the data source of this DbConnection using the specified string for the schema name.
(Overrides DbConnectionGetSchema(String).)
Public methodGetSchema(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).)
Public methodGetSchemaCollection
Gets a schema collection based on the provided restriction values.
Public methodGetSchemaCollectionAsync(String, String)
Asynchronous version of the GetSchemaCollection method.
Public methodGetSchemaCollectionAsync(String, String, CancellationToken)
Asynchronous version of the GetSchemaCollection method.
Protected methodGetService
Returns an object that represents a service provided by the Component or by its Container.
(Inherited from Component.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodInitializeLifetimeService
Obtains a lifetime service object to control the lifetime policy for this instance.
(Inherited from MarshalByRefObject.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodMemberwiseClone(Boolean)
Creates a shallow copy of the current MarshalByRefObject object.
(Inherited from MarshalByRefObject.)
Protected methodOnStateChange
Raises the StateChange event.
(Inherited from DbConnection.)
Public methodCode exampleOpen
Opens a database connection with the property settings specified by the ConnectionString.
(Overrides DbConnectionOpen.)
Public methodOpenAsync
An asynchronous version of Open, which opens a database connection with the settings specified by the ConnectionString. This method invokes the virtual method OpenAsync(CancellationToken) with CancellationToken.None.
(Inherited from DbConnection.)
Public methodOpenAsync(CancellationToken)
This is the asynchronous version of Open. Providers should override with an appropriate implementation. The cancellation token can optionally be honored.The default implementation invokes the synchronous Open call and returns a completed task. The default implementation will return a cancelled task if passed an already cancelled cancellationToken. Exceptions thrown by Open will be communicated via the returned Task Exception property.Do not invoke other methods and properties of the DbConnection object until the returned Task is complete.
(Inherited from DbConnection.)
Public methodPing
Pings the server.
Public methodToString
Returns a String containing the name of the Component, if any. This method should not be overridden.
(Inherited from Component.)
Top
Events
  NameDescription
Public eventDisposed
Occurs when the component is disposed by a call to the Dispose method.
(Inherited from Component.)
Public eventInfoMessage
Occurs when MySQL returns warnings as a result of executing a command or query.
Public eventStateChange
Occurs when the state of the event changes.
(Inherited from DbConnection.)
Top
Remarks

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.

Examples
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>
See Also