Class MySqlCommand
Represents a SQL statement to execute against a MySQL database. This class cannot be inherited.
Inheritance
Implements
Namespace: MySql.Data.MySqlClient
Assembly: MySql.Data.dll
Version: 9.3.0
Syntax
public sealed class MySqlCommand : DbCommand, IComponent, IDbCommand, IAsyncDisposable, IDisposable, ICloneable
Remarks
You can reset the Command
If an exception of type My
You can read more about it here.
note
Using the '@' symbol for paramters is now the preferred approach although the old pattern of using '?' is still supported. Please be aware that using '@' can cause conflicts when user variables are also used. For more information, see the documentation on the AllowUserVariables connection string option.
Constructors
MySqlCommand()
Initializes a new instance of the MySqlCommand class.
Declaration
public MySqlCommand()
Remarks
The base constructor initializes all fields to their default values.
MySqlCommand(String)
Initializes a new instance of the My
Declaration
public MySqlCommand(string cmdText)
Parameters
Type | Name | Description |
---|---|---|
System. |
cmdText | The text of the query. |
MySqlCommand(String, MySqlConnection)
Initializes a new instance of the My
Declaration
public MySqlCommand(string cmdText, MySqlConnection connection)
Parameters
Type | Name | Description |
---|---|---|
System. |
cmdText | The text of the query. |
My |
connection | A My |
MySqlCommand(String, MySqlConnection, MySqlTransaction)
Initializes a new instance of the My
Declaration
public MySqlCommand(string cmdText, MySqlConnection connection, MySqlTransaction transaction)
Parameters
Type | Name | Description |
---|---|---|
System. |
cmdText | The text of the query. |
My |
connection | A My |
My |
transaction | The My |
Properties
Attributes
Gets the My
Declaration
public MySqlAttributeCollection Attributes { get; }
Property Value
Type | Description |
---|---|
My |
The query attributes defined for the statement. The default is an empty collection. |
Remarks
Connector/NET does not support unnamed query attributes. Every query attribute added to the collection must have an associated name. You can read more about it here.
note
Query Attributes can be used along with My
CacheAge
Gets or sets the seconds for how long a TableDirect result should be cached.
Declaration
public int CacheAge { get; set; }
Property Value
Type | Description |
---|---|
System. |
Number of seconds. |
CommandText
Gets or sets the SQL statement to execute at the data source.
Declaration
public override string CommandText { get; set; }
Property Value
Type | Description |
---|---|
System. |
The SQL statement or stored procedure to execute. The default is an empty string. |
Overrides
Remarks
You can read more about it here.
CommandTimeout
Gets or sets the wait time before terminating the attempt to execute a command and generating an error.
Declaration
public override int CommandTimeout { get; set; }
Property Value
Type | Description |
---|---|
System. |
The time (in seconds) to wait for the command to execute. The default is 30 seconds. |
Overrides
Remarks
CommandTimeout is dependent on the ability of MySQL to cancel an executing query.
CommandType
Gets or sets a value indicating how the Command
Declaration
public override CommandType CommandType { get; set; }
Property Value
Type | Description |
---|---|
System. |
One of the System. |
Overrides
Remarks
You can read more about it here.
Connection
Gets or sets the My
Declaration
public MySqlConnection Connection { get; set; }
Property Value
Type | Description |
---|---|
My |
The connection to a data source. The default value is a null reference. |
DbConnection
Gets or sets the Db
Declaration
protected override DbConnection DbConnection { get; set; }
Property Value
Type | Description |
---|---|
System. |
The connection. |
Overrides
DbParameterCollection
Gets the collection of My
Declaration
protected override DbParameterCollection DbParameterCollection { get; }
Property Value
Type | Description |
---|---|
System. |
The collection. |
Overrides
DbTransaction
Gets or sets the Db
Declaration
protected override DbTransaction DbTransaction { get; set; }
Property Value
Type | Description |
---|---|
System. |
The transaction. |
Overrides
DesignTimeVisible
Gets or sets a value indicating whether the command object should be visible in a Windows Form Designer control.
Declaration
[Browsable(false)]
public override bool DesignTimeVisible { get; set; }
Property Value
Type | Description |
---|---|
System. |
True if it should be visible; otherwise, false. |
Overrides
EnableCaching
Gets or sets a System.
Declaration
public bool EnableCaching { get; set; }
Property Value
Type | Description |
---|---|
System. |
True if it is enabled; otherwise, false. |
IsPrepared
Gets a boolean value that indicates whether the Prepare() method has been called.
Declaration
[Browsable(false)]
public bool IsPrepared { get; }
Property Value
Type | Description |
---|---|
System. |
True if it is Prepared; otherwise, false. |
LastInsertedId
Provides the ID of the last inserted row.
Declaration
[Browsable(false)]
public long LastInsertedId { get; }
Property Value
Type | Description |
---|---|
System. |
ID of the last inserted row. -1 if none exists. |
Remarks
An important point to remember is that this property can be used in batch SQL scenarios but it's important to remember that it will only reflect the insert ID from the last insert statement in the batch. This property can also be used when the batch includes select statements and ExecuteReader is used. This property can be consulted during result set processing.
Parameters
Gets the My
Declaration
public MySqlParameterCollection Parameters { get; }
Property Value
Type | Description |
---|---|
My |
The parameters of the SQL statement or stored procedure. The default is an empty collection. |
Remarks
Connector/NET does not support unnamed parameters. Every parameter added to the collection must have an associated name. You can read more about it here.
note
Parameters can be used along with My
Transaction
Gets or sets the instance of My
Declaration
[Browsable(false)]
public MySqlTransaction Transaction { get; set; }
Property Value
Type | Description |
---|---|
My |
The My |
Remarks
You cannot set the Transaction property if it is already set to a
specific value, and the command is in the process of executing. If you set the
transaction to use a My
UpdatedRowSource
Gets or sets how command results are applied to the System.
Declaration
public override UpdateRowSource UpdatedRowSource { get; set; }
Property Value
Type | Description |
---|---|
System. |
One of the System. |
Overrides
Remarks
The default System.
Methods
BeginExecuteNonQuery()
Initiates the asynchronous execution of the SQL statement or stored procedure
that is described by this My
Declaration
[Obsolete]
public IAsyncResult BeginExecuteNonQuery()
Returns
Type | Description |
---|---|
System. |
An System. |
BeginExecuteNonQuery(AsyncCallback, Object)
Initiates the asynchronous execution of the SQL statement or stored procedure
that is described by this My
Declaration
[Obsolete]
public IAsyncResult BeginExecuteNonQuery(AsyncCallback callback, object stateObject)
Parameters
Type | Name | Description |
---|---|---|
System. |
callback | An System. |
System. |
stateObject | A user-defined state object that is passed to the
callback procedure. Retrieve this object from within the callback procedure
using the System. |
Returns
Type | Description |
---|---|
System. |
An System. |
BeginExecuteReader()
Initiates the asynchronous execution of the SQL statement or stored procedure
that is described by this My
Declaration
[Obsolete]
public IAsyncResult BeginExecuteReader()
Returns
Type | Description |
---|---|
System. |
An System. |
BeginExecuteReader(CommandBehavior)
Initiates the asynchronous execution of the SQL statement or stored procedure
that is described by this My
Declaration
[Obsolete]
public IAsyncResult BeginExecuteReader(CommandBehavior behavior)
Parameters
Type | Name | Description |
---|---|---|
System. |
behavior | One of the System. |
Returns
Type | Description |
---|---|
System. |
An System. |
Cancel()
Attempts to cancel the execution of a currently active command
Declaration
public override void Cancel()
Overrides
Clone()
Creates a clone of this My
Declaration
public object Clone()
Returns
Type | Description |
---|---|
System. |
The cloned My |
CreateDbParameter()
Declaration
protected override DbParameter CreateDbParameter()
Returns
Type | Description |
---|---|
System. |
Overrides
CreateParameter()
Creates a new instance of a My
Declaration
public MySqlParameter CreateParameter()
Returns
Type | Description |
---|---|
My |
A My |
Remarks
This method is a strongly-typed version of System.
Dispose()
Releases the resources used by the My
Declaration
public void Dispose()
Dispose(Boolean)
Releases the resources used by the My
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System. |
disposing |
Overrides
DisposeAsync()
Declaration
public override ValueTask DisposeAsync()
Returns
Type | Description |
---|---|
System. |
Overrides
EndExecuteNonQuery(IAsyncResult)
Finishes asynchronous execution of a SQL statement.
Declaration
[Obsolete]
public int EndExecuteNonQuery(IAsyncResult asyncResult)
Parameters
Type | Name | Description |
---|---|---|
System. |
asyncResult | The System. |
Returns
Type | Description |
---|---|
System. |
EndExecuteReader(IAsyncResult)
Finishes asynchronous execution of a SQL statement, returning the requested
My
Declaration
[Obsolete]
public MySqlDataReader EndExecuteReader(IAsyncResult result)
Parameters
Type | Name | Description |
---|---|---|
System. |
result | The System. |
Returns
Type | Description |
---|---|
My |
A MySqlDataReader object that can be used to retrieve the requested rows. |
ExecuteDbDataReader(CommandBehavior)
Declaration
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
Parameters
Type | Name | Description |
---|---|---|
System. |
behavior |
Returns
Type | Description |
---|---|
System. |
Overrides
ExecuteDbDataReaderAsync(CommandBehavior, CancellationToken)
Declaration
protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
System. |
behavior | |
System. |
cancellationToken |
Returns
Type | Description |
---|---|
System. |
Overrides
ExecuteNonQuery()
Executes a SQL statement against the connection and returns the number of rows affected.
Declaration
public override int ExecuteNonQuery()
Returns
Type | Description |
---|---|
System. |
Number of rows affected |
Overrides
Remarks
You can use Execute
ExecuteNonQueryAsync(CancellationToken)
Asynchronous version of Execute
Declaration
public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
System. |
cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
System. |
A task representing the asynchronous operation. |
Overrides
ExecuteReader()
Sends the Command
Declaration
public MySqlDataReader ExecuteReader()
Returns
Type | Description |
---|---|
My |
A My |
Remarks
When the Command
While My
ExecuteReader(CommandBehavior)
Sends the Command
Declaration
public MySqlDataReader ExecuteReader(CommandBehavior behavior)
Parameters
Type | Name | Description |
---|---|---|
System. |
behavior | One of the System. |
Returns
Type | Description |
---|---|
My |
A My |
Remarks
When the Command
If the My
note
When calling ExecuteReader with the SingleRow behavior, you should be aware that using a limit clause in your SQL will cause all rows (up to the limit given) to be retrieved by the client. The Read() method will still return false after the first row but pulling all rows of data into the client will have a performance impact. If the limit clause is not necessary, it should be avoided.
ExecuteReaderAsync(CommandBehavior)
Asynchronous version of Execute
Declaration
public Task<MySqlDataReader> ExecuteReaderAsync(CommandBehavior behavior)
Parameters
Type | Name | Description |
---|---|---|
System. |
behavior | One of the System. |
Returns
Type | Description |
---|---|
System. |
A task representing the asynchronous operation. |
ExecuteReaderAsync(CommandBehavior, CancellationToken)
Asynchronous version of Execute
Declaration
public Task<MySqlDataReader> ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
System. |
behavior | One of the System. |
System. |
cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
System. |
A task representing the asynchronous operation. |
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.
Declaration
public override object ExecuteScalar()
Returns
Type | Description |
---|---|
System. |
The first column of the first row in the result set, or a null reference if the result set is empty |
Overrides
Remarks
Use the ExecuteScalar method to retrieve a single value (for example,
an aggregate value) from a database. This requires less code than using the
Execute
ExecuteScalarAsync(CancellationToken)
Asynchronous version of Execute
Declaration
public override Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
System. |
cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
System. |
A task representing the asynchronous operation. |
Overrides
Finalize()
Declaration
protected void Finalize()
Prepare()
Creates a prepared version of the command on an instance of MySQL Server.
Declaration
public override void Prepare()
Overrides
PrepareAsync(CancellationToken)
Asynchronously creates a prepared version of the command on an instance of MySQL Server.
Declaration
public override Task PrepareAsync(CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
cancellationToken |
Returns
Type | Description |
---|---|
System. |