Datasheet

Property Description
CommandText Contains the SQL command or the name of a stored procedure.
CommandType Indicates the type of command being run, and can be one of the
CommandType enumeration values, which are:
StoredProcedure, to indicate a stored procedure is being run.
TableDirect, to indicate the entire contents of a table are being returned.
In this case, the
CommandText property should contain the table name. This
value only works for Oledb connections.
Text, to indicate a SQL text command. This is the default value.
Connection The Connection object being used to connect to a database.
Parameters A collection or Parameter objects, which are used to pass details to and
from the command.
The three main methods of the command you'll use are the execute methods:
Method Description
ExecuteNonQuery This executes the command but doesn't return any data. It is useful for
commands that perform an action, such as updating data, but doesn't
need to return a value.
ExecuteReader This executes the command and returns a DataReader object.
ExecuteScalar This executes the command and returns a single value.
In the examples so far, we haven't used these methods, as the execution of the command is handled
transparently for us. You'll see the
ExecuteReader method in action when you look at the DataReader,
and the
ExecuteNonQuery method in action in the next chapter.
The Parameters Collection
A parameter is an unknown value – a value that ADO.NET doesn't know until the page is being run,
and is often used to filter data based upon some user value. For example, consider a page showing a list
of products, with a drop down list showing the product categories. The user could select a category so
that only those categories are shown.
The
Parameters collection contains a Parameter object for each parameter in the query. Thus, a
command with three parameters would have objects looking like in Figure 8-19:
277
Reading From Databases
57076_Ch 8 SAN.qxd 01/12/2003 6:43 PM Page 277