[ Team LiB ] |
20.2 Properties Reference
Specifies the data type of the parameter, using the DbType enumeration in the System.Data namespace. This enumeration includes values for common data types, such as strings and numbers of various sizes. If this property is set, the value of the Parameter is converted to this type before it is passed to the data source. If the type isn't specified, ADO.NET attempts to infer the data source type by looking at the .NET type. ExampleThe following code snippet sets the Parameter.DbType property: param.DbType = DbType.Int32; NoteEvery Parameter class includes a DbType property and a provider-specific data type property such as SqlDbType and OleDbType. These two properties are linked. When you set the provider-specific type, the DbType is adjusted to use a compatible type and vice versa. Usually, you will set this provider-specific type, but you may want to use the DbType when writing generic ADO.NET code.
Specifies whether the parameter is input-only, output-only, bidirectional, or a return value. You set the Direction property using a value from the ParameterDirection enumeration, as shown in Table 20-2.
NoteIf a value isn't returned for an output parameter or return value, the Value of the corresponding Parameter is null. With the OLE DB provider, make sure you add a parameter for the return value (if needed) before other parameters.
This Boolean parameter indicates whether null values are accepted for the Parameter.Value property. The default is false. NoteIf your parameter accepts null values, you can test for them using the System.DBNull class. In this case, DBNull.Value equals Parameter.Value.
Sets an offset to use for the Value property, provided the Value property contains a binary or string type. For example, if you set the Offset to 5, the Parameter submits the content from the Value property, starting with the sixth character (in the case of a string) or sixth byte (in the case of binary data). The default is 0 (or no offset). NoteThe Offset property is rarely used, but it lets you submit a portion of a large byte array (for example, when dealing with image processing).
Specifies the OLE DB data type of the parameter, using the OleDbType enumeration in the System.Data.OleDb namespace. The value of the Parameter is converted into this type before it is passed to the data source. The default is a DbType.VarWChar, a variable length string of Unicode characters. ExampleThe following code snippet sets the Parameter.OleDbType property and then examines the linked DbType. In this case, the linked DbType value is DbType.Int32. param.OleDbType = OleDbType.Integer; Console.WriteLine(param.DbType.ToString()); NotesEvery OleDbParameter class includes a DbType property and a provider-specific OleDbType property. These two properties are linked. When you set the OleDbType property, the DbType is adjusted to use a compatible type and vice versa. For more information about valid OLE DB data types and their mappings to .NET framework types, see Appendix A.
Names the parameter with a string. When calling a stored procedure, you must make sure that the ParameterName property exactly matches the parameter name defined in the data source. The ParameterName is also used as an indexer for the ParameterCollection (as is the ordinal position in the collection) and can retrieve Parameter objects, check if they exist in the collection, or remove them. ExampleThe following code snippet creates a parameter named @Description, and then retrieves it from the collection by name. cmd.Parameters.Add("@Description", SqlDbType.VarChar, 88); SqlParameter param = cmd.Parameters["@Description"]; NotesThe ParameterName can also insert values into parameterized commands used with the SQL Server provider. However, the name doesn't play an important role when creating parameterized commands with the OLE DB provider. With the OLE DB provider, all parameters are identified with question-mark placeholders, and the order alone determines which parameters to substitute. For a full example of both SQL Server and OLE DB parameterized commands, refer to Chapter 4. Parameter names typically begin with the @ character (as in @CustomerID). This is a common convention, but it isn't necessary.
Determines the maximum number of digits that represents the Value property, assuming it is a numeric type. For example, the number 1234.56 has a precision of 6. The default precision is 0, which indicates no maximum. NoteYou don't need to set the Precision property. The primary reason to set Precision is to ensure you don't accidentally submit a number that has a higher precision than allowed by the data source. In SQL Server, you can set a precision for all decimal data types when creating a table.
Determines the maximum number of digits to the right of the decimal in the Value property, assuming it is a numeric type. For example, the number 1234.56 has a scale of 2. The default scale is 0, which indicates no maximum. NoteYou don't need to set the Scale property. The primary reason to set Scale is to ensure you don't accidentally submit a number that has a higher scale than allowed by the data source. In SQL Server, you can set the scale for all decimal data types when creating a table.
Determines the maximum size of the Value property for a binary or string parameter. For fixed-width data types, the Size property is ignored. If not set, the actual size of the specified parameter value is used In the case of a Unicode string, the Size corresponds to a number of characters, not including the final null termination. In the case of binary data or an ANSI string, the Size refers to a number of bytes. If you set the Size property to a value smaller than the parameter data, the Value is truncated.
Sets the name of the linked column from the DataSet. This property is important when using the DataAdapter to apply updates. In the case of an input parameter, the value from the source column in the DataSet is copied to the parameter value before the command is executed. In the case of an output parameter, the parameter value is copied to the DataSet column after the command is executed, assuming the Command.UpdateRowSource property allows it. ExampleThe following code defines a linked @CategoryID output parameter. After the command is executed, the Value from the parameter is copied into the CategoryID column in the DataSet. param = cmdInsert.Parameters.Add("@CategoryID", SqlDbType.Int); param.SourceColumn = "CategoryID"; param.Direction = ParameterDirection.Output; NoteWhen using the SourceColumn property with an input parameter, the SourceVersion property must indicate the version of the column data you want to use. Its default is DataRowVersion.Current.
Sets the DataRowVersion of the data that is used from the linked column in the DataSet. This property is used when the DataAdapter applies an update. In the case of an input parameter, the value from the source column in the DataSet is copied to the parameter value before the command is executed. The SourceVersion property doesn't apply to output or return-value parameters. The list of valid DataRowVersion values is shown in Table 20-3. Typically, you use the Current value to apply a new change (for example, in the SET statement of a SQL UPDATE command), and the Original value to find a specific row (for example, in the WHERE clause of a SQL command).
ExampleThe following code defines a linked @CategoryName input parameter. Before the command is executed, the current value from the CategoryName field is copied to the Parameter.Value property. param = cmdInsert.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15); param.SourceColumn = "CategoryName"; // The following line could be omitted, as this is the default. param.SourceVersion = DataRowVersion.Current;
Specifies the SQL Server data type of the parameter, using the SqlDbType enumeration in the System.Data namespace. If this property is set, the value of the Parameter is converted to this type before it's passed to the data source. The default is a SqlDbType.NVarChar, a variable-length string of Unicode characters. ExampleThe following code snippet sets the Parameter.SqlDbType property and then examines the linked DbType. In this case, the linked DbType value is DbType.Int32. param.SqlDbType = SqlDbType.Int; Console.WriteLine(param.DbType.ToString()); NotesEvery SqlParameter class includes a DbType property and a provider-specific SqlDbType property. These two properties are linked. When you set the SqlDbType property, the DbType uses a compatible type and vice versa. For more information about valid SQL Server data types, and their mappings to .NET framework types, see Appendix A.
This is the actual content stored in the parameter. For output parameters, this is the information returned after the command is complete. For input parameters, this is the value that is sent to the data source when the command is executed. The Value is stored as a loosely typed object, so it can accommodate any data type. When the command is executed, all input parameter values are converted to the appropriate type by examining the provider-specific type property (such as SqlDbType or OleDbType). This conversion can result in an error, particularly if the type doesn't support the IConvertible interface. ExampleThe following code snippet creates a new parameter (as a variable-length Unicode string) and then sets the Value property using the .NET string type: SqlParameter param; param = new SqlParameter("@Description", SqlDbType.VarChar, 88); param.Value = "This is the description"; NoteIf you wish to specify a null value for a parameter, you must set the Parameter.Value property using the DBNull.Value property, which represents a null value. You can't set it directly to a null reference, or an error will occur. |
[ Team LiB ] |