DekGenius.com
[ Team LiB ] Previous Section Next Section

19.3 Collections Reference

Parameters

ParameterCollection col = Command.Parameters;

If the Command contains a parameterized query or a stored procedure call, the Parameters collection will contain a group of Parameter objects, one for each input and output parameter. In addition, if the stored procedure uses a return value, this should be the first Parameter object in the collection. Parameter objects are described in Chapter 20.

Because the Parameter object is provider-specific, the Parameters collection is also provider-specific and implements IDataParameterCollection. The IDataParameter-Collection defines a small set of members, including a default indexer that allows you to retrieve Parameter objects by their assigned names.

Example

The provider-specific versions of the Parameters collection (such as SqlParameterCollection and OleDbParameterCollection) typically include several overloaded versions of the Add( ) method, which allow you to create and insert a Parameter object in one step.

Here's how to create and set a SqlParameter using the SqlParameter constructor:

SqlParameter param = new SqlParameter("@Description", SqlDbType.VarChar,
                                       88, "Description");
param.Value = "This is the description";

cmd.Add(param);

Here's how to create and set the same SqlParameter using the SqlParameterCollection.Add( ) method:

SqlParameter param = cmd.Add("@Description", SqlDbType.VarChar,
                             88, "Description");

param.Value = "This is the description";
    [ Team LiB ] Previous Section Next Section