dotConnect for MySQL Documentation
Devart.Data.MySql Namespace / MySqlConnection Class
Members Example

In This Topic
    MySqlConnection Class
    In This Topic
    Represents an open connection to MySQL.
    Syntax
    Remarks

    A MySqlConnection object represents a unique connection to MySQL. Use it in conjunction with MySqlCommand, MySqlDataReader, MySqlDataAdapter or other components for convenient interoperation with MySQL.

    When you create an instance of MySqlConnection, all properties are set to their initial values. For a list of these values, see ConnectionString.

    If the MySqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close.

    Note that MySqlConnection instance is not guaranteed to be thread safe. You should avoid using the same MySqlConnection in several threads at the same time. It is recommended to open a new connection per thread and to close it when the work is done. Actually, connections will not be created/disposed every time with the Pooling=true; connection string option - connections will be stored at connection pool. This boosts performance greatly.

    A single MySqlConnection can be used by many MySqlCommand objects on the assumption that all operations will be done consecutively. In other words, you can not execute a SQL statement while an asynchronous operation is in progress.

    This class supports cross-form data binding with the InterForm Technology.

    Example
    The following example creates a MySqlCommand and a MySqlConnection. The MySqlConnection is opened and set as the Connection property. The example then calls ExecuteNonQuery method, and closes the connection. To accomplish this, the ExecuteNonQuery is passed a connection string and a query string that is SQL INSERT statement.
    public void InsertRow(string myConnectionString)
    {
      // If the connection string is empty, use default.
      if(myConnectionString == "")
      {
        myConnectionString = 
            "User Id=root;Host=localhost;Database=Test;";
      }
      MySqlConnection myConn = new MySqlConnection(myConnectionString);
      string myInsertQuery = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')";
      MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
      myCommand.Connection = myConn;
      myConn.Open();
      try
      {
        myCommand.ExecuteNonQuery();
      }
      finally
      {
        myConn.Close();
      }
    }
    Public Sub InsertRow(myConnectionString As String)
      ' If the connection string is empty, use default.
      If myConnectionString = "" Then
        myConnectionString = _
            "User Id=root;Host=localhost;Database=Test;"
      End If
      Dim myConn As New MySqlConnection(myConnectionString)
      Dim myInsertQuery As String = "INSERT INTO DEPT VALUES (50,'Development','Philadelphia')"
      Dim myCommand As New MySqlCommand(myInsertQuery)
      myCommand.Connection = myConn
      myConn.Open()
      Try
        myCommand.ExecuteNonQuery()
      Finally
        myConn.Close()
      End Try
    End Sub
    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.ComponentModel.Component
             System.Data.Common.DbConnection
                Devart.Common.DbConnectionBase
                   Devart.Data.MySql.MySqlConnection

    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also