Documentation Home
MySQL Connector/NET Developer Guide
Related Documentation Download this Manual

5.3 Using MySqlCommand

The MySqlCommand class has the CommandText and CommandType properties associated with it. The CommandText property is interpreted differently, depending on how you set the CommandType property. The following CommandType types are permitted:

  • Text - An SQL text command (default).

  • StoredProcedure - Name of a stored procedure.

  • TableDirect - Name of a table.

The default CommandType type, Text, is used for executing queries and other SQL commands. See Section 6.1.2, “The MySqlCommand Object” for usage examples.

If CommandType is set to StoredProcedure, set CommandText to the name of the stored procedure to access. For use-case examples of the CommandType property with type StoredProcedure, see Section 5.6, “Creating and Calling Stored Procedures”.

If CommandType is set to TableDirect, all rows and columns of the named table are 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 to query. This usage is illustrated by the following code snippet:

MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "mytable";
cmd.Connection = someConnection;
cmd.CommandType = CommandType.TableDirect;
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
   Console.WriteLn(reader[0], reader[1]...);

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 seconds. Avoid a value of 0, which indicates an indefinite wait. To change the default command timeout, use the connection string option Default Command Timeout.

Connector/NET supports 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.

Further details on this can be found in the relevant Microsoft documentation.