MySqlConnection Class

MySQL Connector/Net

MySqlConnection Class
Represents an open connection to a MySQL Server database. This class cannot be inherited.
Inheritance Hierarchy
SystemObject  SystemMarshalByRefObject
    System.ComponentModelComponent
      System.Data.CommonDbConnection
        MySql.Data.MySqlClientMySqlConnection

Namespace: MySql.Data.MySqlClient
Assembly: MySql.Data (in MySql.Data.dll) Version: 6.9.9
Syntax
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.

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
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
Async version of BeginTransaction
Public methodBeginTransactionAsync(IsolationLevel)
Async version of BeginTransaction
Public methodBeginTransactionAsync(CancellationToken)
Public methodBeginTransactionAsync(IsolationLevel, CancellationToken)
Public methodCancelQuery
Public methodCode exampleChangeDatabase
Changes the current database for an open MySqlConnection.
(Overrides DbConnectionChangeDatabase(String).)
Public methodChangeDataBaseAsync(String)
Public methodChangeDataBaseAsync(String, CancellationToken)
Async version of ChangeDataBase
Public methodStatic memberClearAllPools
Clears all connection pools.
Public methodClearAllPoolsAsync
Async version of ClearAllPools
Public methodClearAllPoolsAsync(CancellationToken)
Public methodStatic memberClearPool
Empties the connection pool associated with the specified connection.
Public methodClearPoolAsync(MySqlConnection)
Async version of ClearPool
Public methodClearPoolAsync(MySqlConnection, CancellationToken)
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
Async version of Close
Public methodCloseAsync(CancellationToken)
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
Protected methodDispose(Boolean) (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 the default hash function.
(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
Public methodGetSchemaCollectionAsync(String, String)
Async version of GetSchemaCollection
Public methodGetSchemaCollectionAsync(String, String, CancellationToken)
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 (Inherited from DbConnection.)
Public methodCode exampleOpen
Opens a database connection with the property settings specified by the ConnectionString.
(Overrides DbConnectionOpen.)
Public methodOpenAsync (Inherited from DbConnection.)
Public methodOpenAsync(CancellationToken) (Inherited from DbConnection.)
Public methodPing
Ping
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 (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>
<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.

See Also