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