Name

IDbCommand

Synopsis

The IDbCommand interface represents a command that can be executed against a data source. Some examples include queries that retrieve rows (a SQL SELECT statement), statements that retrieve a single piece of information (such as SQL aggregate functions), or statements designed to modify rows (such as a SQL UPDATE, DELETE, or INSERT statement). ADO.NET providers that access relational data implement this interface.

Before, using the command, you must set the Connection property to a valid IDbConnection and open the connection. You can then execute the command in one of three ways. Use ExecuteNonQuery( ) to execute a statement such as INSERT, DELETE, or UPDATE, which returns the number of rows affected. Use ExecuteScalar( ) to execute a statement that returns a single value, such as an aggregate SQL function (SUM, MAX, MIN, AVG, and so on). Finally, use ExecuteReader( ) to perform a query and return a IDataReader that provides access to the result set. The CommandTimeout property identifies the number of seconds ADO.NET will wait while trying to execute the command before throwing a provider-specific exception. The default is 30, and 0 represents an infinite wait. Finally, if you are using a stored procedure, you can use the Prepare( ) method to compile the stored procedure in the data source, which may improve performance with some providers if you are executing the same stored procedure multiple times with different parameters. (It’s recommended that you profile ...

Get ADO.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.