Commands are issued against databases to take actions against data stores. For example, you could execute a command that inserts or deletes data. For more information on moving data into and out of databases
To ih23h3ssue a command against a database, the Command object must have two basic things: a Connection and CommandText, both of which can be set in the constructor. To execute the command, the Connection has to be open and not in fetching state:
String InsertCmdString; InsertCmdString = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"; SqlCommand mySqlCommand = new SqlCommand(InsertCmdString, myConnection);
SqlCommand features the following methods for executing commands at a SQL Server database:
|ExecuteReader||Executes commands that return rows. For increased performance, ExecuteReader invokes commands using the Transact-SQL sp_executesql system stored procedure. Therefore, ExecuteReader might not have the effect that you want if used to execute commands such as Transact-SQL SET statements.|
|ExecuteNonQuery||Executes commands such as Transact-SQL INSERT, DELETE, UPDATE, and SET statements.|
|ExecuteScalar||Retrieves a single value (for example, an aggregate value) from a database.|
To execute the command that does not return results, call the ExecuteNonQuery method.