Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

26.2.3.3. The MySqlConnection Class

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.

26.2.3.3.1. Properties

The following properties are available:

  • ConnectionString: Gets or sets the string used to connect to a MySQL Server database.

  • ConnectionTimeout: Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.

  • Database: Gets the name of the current database or the database to be used after a connection is opened.

  • DataSource: Gets the name of the MySQL server to which to connect.

  • 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.

  • State: Gets the current state of the connection.

  • UseConnection: Indicates if this connection should use compression when communicating with the server.

26.2.3.3.2. Methods

The following methods are available:

  • BeginTransaction: Begins a database transaction.

  • ChangeDatabase: Changes the current database for an open MySqlConnection.

  • Close: Closes the connection to the database. This is the preferred method of closing any open connection.

  • CreateCommand: Creates and returns a MySqlCommand object associated with the MySqlConnection.

  • Dispose: Releases the resources used by the MySqlConnection.

  • Open: Opens a database connection with the property settings specified by the ConnectionString.

  • Ping: Pings the MySQL server.

26.2.3.3.3. Usage

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 an SQL INSERT statement.

26.2.3.3.3.1. VB.NET

The following example shows how to use the MySqlConnection class with VB.NET:

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
      
26.2.3.3.3.2. C#

The following example shows how to use the MySqlConnection class with 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();
}

      

 
 
  Published under the terms of the GNU General Public License Design by Interspire