MySqlCommand Class

MySQL Connector/Net

Represents a SQL statement to execute against a MySQL database. This class cannot be inherited.

Namespace:  MySql.Data.MySqlClient
Assembly:  MySql.Data (in MySql.Data.dll) Version: 6.2.2.0

Syntax

C#
public sealed class MySqlCommand : DbCommand, 
	ICloneable
Visual Basic (Declaration)
Public NotInheritable Class MySqlCommand _
	Inherits DbCommand _
	Implements ICloneable
Visual C++
public ref class MySqlCommand sealed : public DbCommand, 
	ICloneable

Remarks

MySqlCommand features the following methods for executing commands at a MySQL database:
ItemDescription
ExecuteReaderExecutes commands that return rows.
ExecuteNonQueryExecutes commands such as SQL INSERT, DELETE, and UPDATE statements.
ExecuteScalarRetrieves a single value (for example, an aggregate value) from a database.
You can reset the CommandText property and reuse the MySqlCommand object. However, you must close the MySqlDataReader before you can execute a new or previous command. If a MySqlException is generated by the method executing a MySqlCommand, the MySqlConnection remains open. It is the responsibility of the programmer to close the connection.
Note: Using the '@' symbol for paramters is now the preferred approach although the old pattern of using '?' is still supported. Please be aware though that using '@' can cause conflicts when user variables are also used. To help with this situation please see the documentation on the 'allow user variables' connection string option. The 'old syntax' connection string option has now been deprecated.

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.
CopyVB.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
CopyC#
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();
}

Inheritance Hierarchy

System..::.Object
  System..::.MarshalByRefObject
    System.ComponentModel..::.Component
      System.Data.Common..::.DbCommand
        MySql.Data.MySqlClient..::.MySqlCommand

See Also