CreateParameter
IDbDataParameter param = Command.CreateParameter();
Returns a strongly typed provider-specific
IDbDataParameter
object. This method is primarily
useful if you are writing generic code because it
doesn’t force you to explicitly differentiate your
code based on the type of Parameter
object.
However, this approach can be restrictive because it prevents you
from using data types that may be specific to your data source (you
must use the closest type from the
System.Data.DbType
enumeration instead). Also,
unlike the Parameter
object constructors and the
ParameterCollection.Add()
method, the
CreateParameter( )
method doesn’t
accept any parameters, which means that you need to specify the name,
data type, length, and so on, using separate property set statements.
The following code snippet uses the CreateParameter( )
method to generically create a
Parameter
object and then configures it
accordingly:
IDbDataParameter param = cmd.CreateParameter(); param.Name = "@Description"; param.DbType = DbType.VarWChar; param.Size = 88; param.Value = "This is the description"; cmd.Add(param);