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.1. The MySqlCommand Class

The MySqlCommand class represents an SQL statement to execute against a MySQL database.

Note: Prior versions of the provider used the '@' symbol to mark parameters in SQL. This is incompatible with MySQL user variables, so the provider now uses the '?' symbol to locate parameters in SQL. To support older code, you can set 'old syntax=yes' in your connection string. If you do this, please be aware that an exception will not be thrown if you fail to define a parameter that you intended to use in your SQL.

26.2.3.1.1. Properties

The following properties are available:

  • CommandText: Gets or sets the SQL statement to execute at the data source.

  • CommandTimeout: Gets or sets the wait time before terminating the attempt to execute a command and generating an error.

  • CommandType: Gets or sets a value indicating how the CommandText property is to be interpreted. Possible types are StoredProcedure, TableDirect, and Text.

  • Connection: Gets or sets the MySqlConnection used by this instance of the MySqlCommand.

  • IsPrepared: Is true if this command has been prepared, false otherwise.

  • Parameters: Gets the MySqlParameterCollection.

  • Transaction: Gets or sets the MySqlTransaction within which the MySqlCommand executes.

  • UpdatedRowSource: Gets or sets how command results are applied to the DataRow when used by the Update method of the DbDataAdapter.

26.2.3.1.2. Methods

The following methods are available:

  • Cancel: Attempts to cancel the execution of a MySqlCommand. This operation is not supported.

  • Clone: Creates a clone of this MySqlCommand object. CommandText, Connection, and Transaction properties are included as well as the entire parameter list.

  • CreateParameter: Creates a new instance of a MySqlParameter object.

  • Dispose: Disposes of this instance of MySqlCommand.

  • ExecuteNonQuery: Executes an SQL statement against the connection and returns the number of rows affected.

  • ExecuteReader: Sends the CommandText to the Connection and builds a MySqlDataReader.

  • ExecuteScalar: Executes the query, and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.

  • Prepare: Creates a prepared version of the command on an instance of MySQL Server.

26.2.3.1.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.1.3.1. VB.NET

The following example show how to use the MySqlCommand 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.1.3.2. C#

The following example show how to use the MySqlCommand 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