See Also

MySqlConnection Members  | Devart.Data.MySql Namespace  | MySqlDataAdapter Class  | MySqlCommand Class

 

Language

Visual Basic

C#

Show All

See AlsoRequirementsLanguagesDevart.Data.MySqlSend comments on this topic.

MySqlConnection Class

Represents an open connection to a MySQL database.

For a list of all members of this type, see MySqlConnection members.

Inheritance Hierarchy

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

Syntax

[Visual Basic]
Public Class MySqlConnection    Inherits DbConnectionBase    Implements IComponentIDbConnectionICloneableIDisposable 
[C#]
public class MySqlConnection : DbConnectionBase, IComponentIDbConnectionICloneableIDisposable 

Remarks

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

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

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

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.

[C#] 

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 Test.Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')"; 
  MySqlCommand myCommand = new MySqlCommand(myInsertQuery); 
  myCommand.Connection = myConn; 
  myConn.Open(); 
  try 
  { 
    myCommand.ExecuteNonQuery(); 
  } 
  finally 
  { 
    myConn.Close(); 
  } 
}

[Visual Basic] 

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 Test.Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')"
  Dim myCommand As New MySqlCommand(myInsertQuery)
  myCommand.Connection = myConn
  myConn.Open()
  Try
    myCommand.ExecuteNonQuery()
  Finally
    myConn.Close()
  End Try
End Sub

 

See Also

MySqlConnection Members  | Devart.Data.MySql Namespace  | MySqlDataAdapter Class  | MySqlCommand Class

 

 


© 2002 - 2008 Devart. All Rights Reserved.