Build command objects and query data from data sources
DbCommand
You use the DbCommand object to send a SQL command to the data store.
DbCommand
can be a Data Manipulation Language (DML) command to retrieve, insert, update, or delete data, as well as Data Definition Language (DDL) command, which enables you to create tables and modify schema information at the database.
Requirements
The DbCommand
object requires:
valid open connection
valid value for its
CommandText
propertyvalid value for its
CommandType
property
How to create
pass a
DbConnection
object into theDbCommand
object’s constructorattach a
DbConnection
object to the existingDbCommand
object’sConnection
property(the best way) use the
CreateCommand
method on theDbConnection
object
Example
Methods
ExecuteNonQuery. Use it when you don’t expect a command to return any rows—an insert, update, or delete query, for example.
ExecuteReader. It returns a
DbDataReader
instance, which is a forward-only, read-only, server-side cursor.ExecuteScalar. Queries are often expected to return a single row with a single column. In these situations, the results can be treated as a single return value.
Last updated