Databases Reference
In-Depth Information
CHAPTER 11
Executing Commands
O nce you've established a connection to the database, you want to start interacting
with it and getting it doing something useful for you. You may need to add, update, or
delete some data, or perhaps modify the database in some other way, usually by running
a query. Whatever the task, it will inevitably involve a command .
In this chapter, we'll explain commands, which are objects that encapsulate the
SQL for the action you want to perform and that provide methods for submitting it to
the database. Each data provider has a command class that implements the System.Data.
IDbCommand interface.
In this chapter, we'll cover the following:
• Creating commands
• Executing commands
• Executing commands with multiple results
• Executing statements
• Command parameters
We'll use the SQL Server data provider ( System.Data.SqlClient ) in our examples. Its
command is named SqlCommand . The commands for the other data providers work the
same way.
Creating a Command
You can create a command either using the SqlCommand constructor or using methods that
create the object for you. Let's look at the first of these alternatives.
209
Search WWH ::




Custom Search