A MySqlCommand has the CommandText
and
CommandType
properties associated with it. The
CommandText
will be handled differently
depending on the setting of CommandType
.
CommandType
can be one of:
Text - A SQL text command (default)
StoredProcedure - The name of a Stored Procedure
TableDirect - The name of a table (new in Connector/NET 6.2)
The default CommandType
,
Text
, is used for executing queries and other
SQL commands. Some example of this can be found in the following
section Section 22.2.4.1.2, “The MySqlCommand Object”.
If CommandType
is set to
StoredProcedure
, CommandText
should be set to the name of the Stored Procedure to access.
If CommandType
is set to
TableDirect
, all rows and columns of the named
table will be returned when you call one of the Execute methods.
In effect, this command performs a SELECT *
on
the table specified. The CommandText
property
is set to the name of the table you wish to query. This is
illustrated by the following code snippet:
... MySqlCommand cmd = new MySqlCommand(); cmd.CommandText = "mytable"; cmd.Connection = someConnection; cmd.CommandType = CommandType.TableDirect; SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLn(reader[0], reader[1]...); } ...
Examples of using the CommandType of StoredProcedure can be found in the section Section 22.2.5.6, “Accessing Stored Procedures with Connector/NET”.
Commands can have a timeout associated with them. This is useful
as you may not want a situation were a command takes up an
excessive amount of time. A timeout can be set using the
CommandTimeout
property. The following code
snippet sets a timeout of one minute:
MySqlCommand cmd = new MySqlCommand(); cmd.CommandTimeout = 60;
The default value is 30 secs. A value of 0 indicates an indefinite
wait and should be avoided. Note the default command timeout can
be changed using the connection string option Default
Command Timeout
.
Prior to MySQL Connector/NET 6.2,
MySqlCommand.CommandTimeout
included user
processing time, that is processing time not related to direct use
of the connector. Timeout was implemented through a .NET Timer,
that triggered after CommandTimeout
seconds.
This timer consumed a thread.
MySQL Connector/NET 6.2 introduced timeouts that are aligned with how Microsoft
handles SqlCommand.CommandTimeout
. This
property is the cumulative timeout for all network reads and
writes during command execution or processing of the results. A
timeout can still occur in the MySqlReader.Read
method after the first row is returned, and does not include user
processing time, only IO operations. The 6.2 implementation uses
the underlying stream timeout facility, so is more efficient in
that it does not require the additional timer thread as was the
case with the previous implementation.
Further details on this can be found in the relevant Microsoft documentation.