SqlParameter SqlParameter SqlParameter SqlParameter Class

Definition

Represents a parameter to a SqlCommand and optionally its mapping to DataSet columns. This class cannot be inherited. For more information on parameters, see Configuring Parameters and Parameter Data Types.

public ref class SqlParameter sealed : System::Data::Common::DbParameter, ICloneable
[System.ComponentModel.TypeConverter("System.Data.SqlClient.SqlParameter+SqlParameterConverter, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
[System.ComponentModel.TypeConverter(typeof(System.Data.SqlClient.SqlParameterConverter))]
[System.ComponentModel.TypeConverter(typeof(System.Data.SqlClient.SqlParameter/SqlParameterConverter))]
[System.ComponentModel.TypeConverter("System.Data.SqlClient.SqlParameter+SqlParameterConverter, System.Data, Version=2.0.5.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public sealed class SqlParameter : System.Data.Common.DbParameter, ICloneable
type SqlParameter = class
    inherit DbParameter
    interface IDbDataParameter
    interface IDataParameter
    interface ICloneable
Public NotInheritable Class SqlParameter
Inherits DbParameter
Implements ICloneable
Inheritance
Attributes
Implements

Examples

The following example creates multiple instances of SqlParameter through the SqlParameterCollection collection within the SqlDataAdapter. These parameters are used to select data from the data source and put the data in the DataSet. This example assumes that a DataSet and a SqlDataAdapter have already been created by using the appropriate schema, commands, and connection. For more information and additional examples on using parameters, see Retrieving and Modifying Data in ADO.NET and Configuring Parameters and Parameter Data Types.

public void AddSqlParameters() 
{
// ...
// create categoriesDataSet and categoriesAdapter
// ...

  categoriesAdapter.SelectCommand.Parameters.Add(
    "@CategoryName", SqlDbType.VarChar, 80).Value = "toasters";
  categoriesAdapter.SelectCommand.Parameters.Add(
    "@SerialNum", SqlDbType.Int).Value = 239;
  categoriesAdapter.Fill(categoriesDataSet);

}
Public Sub AddSqlParameters()
    ' ...
    ' create categoriesDataSet and categoriesAdapter
    ' ...
    categoriesAdapter.SelectCommand.Parameters.Add( _
        "@CategoryName", SqlDbType.VarChar, 80).Value = "toasters"
    categoriesAdapter.SelectCommand.Parameters.Add( _
        "@SerialNum", SqlDbType.Int).Value = 239
    
    categoriesAdapter.Fill(categoriesDataSet)
End Sub  

Remarks

Parameter names are not case sensitive.

Note

Nameless, also called ordinal, parameters are not supported by the .NET Framework Data Provider for SQL Server.

For more information, along with additional sample code demonstrating how to use parameters, see Commands and Parameters.

Constructors

SqlParameter() SqlParameter() SqlParameter() SqlParameter()

Initializes a new instance of the SqlParameter class.

SqlParameter(String, Object) SqlParameter(String, Object) SqlParameter(String, Object) SqlParameter(String, Object)

Initializes a new instance of the SqlParameter class that uses the parameter name and a value of the new SqlParameter.

SqlParameter(String, SqlDbType) SqlParameter(String, SqlDbType) SqlParameter(String, SqlDbType) SqlParameter(String, SqlDbType)

Initializes a new instance of the SqlParameter class that uses the parameter name and the data type.

SqlParameter(String, SqlDbType, Int32) SqlParameter(String, SqlDbType, Int32) SqlParameter(String, SqlDbType, Int32) SqlParameter(String, SqlDbType, Int32)

Initializes a new instance of the SqlParameter class that uses the parameter name, the SqlDbType, and the size.

SqlParameter(String, SqlDbType, Int32, ParameterDirection, Boolean, Byte, Byte, String, DataRowVersion, Object) SqlParameter(String, SqlDbType, Int32, ParameterDirection, Boolean, Byte, Byte, String, DataRowVersion, Object) SqlParameter(String, SqlDbType, Int32, ParameterDirection, Boolean, Byte, Byte, String, DataRowVersion, Object) SqlParameter(String, SqlDbType, Int32, ParameterDirection, Boolean, Byte, Byte, String, DataRowVersion, Object)

Initializes a new instance of the SqlParameter class that uses the parameter name, the type of the parameter, the size of the parameter, a ParameterDirection, the precision of the parameter, the scale of the parameter, the source column, a DataRowVersion to use, and the value of the parameter.

SqlParameter(String, SqlDbType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object, String, String, String) SqlParameter(String, SqlDbType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object, String, String, String) SqlParameter(String, SqlDbType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object, String, String, String) SqlParameter(String, SqlDbType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object, String, String, String)

Initializes a new instance of the SqlParameter class that uses the parameter name, the type of the parameter, the length of the parameter the direction, the precision, the scale, the name of the source column, one of the DataRowVersion values, a Boolean for source column mapping, the value of the SqlParameter, the name of the database where the schema collection for this XML instance is located, the owning relational schema where the schema collection for this XML instance is located, and the name of the schema collection for this parameter.

SqlParameter(String, SqlDbType, Int32, String) SqlParameter(String, SqlDbType, Int32, String) SqlParameter(String, SqlDbType, Int32, String) SqlParameter(String, SqlDbType, Int32, String)

Initializes a new instance of the SqlParameter class that uses the parameter name, the SqlDbType, the size, and the source column name.

Properties

CompareInfo CompareInfo CompareInfo CompareInfo

Gets or sets the CompareInfo object that defines how string comparisons should be performed for this parameter.

DbType DbType DbType DbType

Gets or sets the SqlDbType of the parameter.

Direction Direction Direction Direction

Gets or sets a value that indicates whether the parameter is input-only, output-only, bidirectional, or a stored procedure return value parameter.

ForceColumnEncryption ForceColumnEncryption ForceColumnEncryption ForceColumnEncryption

Enforces encryption of a parameter when using Always Encrypted. If SQL Server informs the driver that the parameter does not need to be encrypted, the query using the parameter will fail. This property provides additional protection against security attacks that involve a compromised SQL Server providing incorrect encryption metadata to the client, which may lead to data disclosure.

IsNullable IsNullable IsNullable IsNullable

Gets or sets a value that indicates whether the parameter accepts null values. IsNullable is not used to validate the parameter’s value and will not prevent sending or receiving a null value when executing a command.

LocaleId LocaleId LocaleId LocaleId

Gets or sets the locale identifier that determines conventions and language for a particular region.

Offset Offset Offset Offset

Gets or sets the offset to the Value property.

ParameterName ParameterName ParameterName ParameterName

Gets or sets the name of the SqlParameter.

Precision Precision Precision Precision

Gets or sets the maximum number of digits used to represent the Value property.

Scale Scale Scale Scale

Gets or sets the number of decimal places to which Value is resolved.

Size Size Size Size

Gets or sets the maximum size, in bytes, of the data within the column.

SourceColumn SourceColumn SourceColumn SourceColumn

Gets or sets the name of the source column mapped to the DataSet and used for loading or returning the Value

SourceColumnNullMapping SourceColumnNullMapping SourceColumnNullMapping SourceColumnNullMapping

Sets or gets a value which indicates whether the source column is nullable. This allows SqlCommandBuilder to correctly generate Update statements for nullable columns.

SourceVersion SourceVersion SourceVersion SourceVersion

Gets or sets the DataRowVersion to use when you load Value

SqlDbType SqlDbType SqlDbType SqlDbType

Gets or sets the SqlDbType of the parameter.

SqlValue SqlValue SqlValue SqlValue

Gets or sets the value of the parameter as an SQL type.

TypeName TypeName TypeName TypeName

Gets or sets the type name for a table-valued parameter.

UdtTypeName UdtTypeName UdtTypeName UdtTypeName

Gets or sets a string that represents a user-defined type as a parameter.

Value Value Value Value

Gets or sets the value of the parameter.

XmlSchemaCollectionDatabase XmlSchemaCollectionDatabase XmlSchemaCollectionDatabase XmlSchemaCollectionDatabase

Gets the name of the database where the schema collection for this XML instance is located.

XmlSchemaCollectionName XmlSchemaCollectionName XmlSchemaCollectionName XmlSchemaCollectionName

Gets the name of the schema collection for this XML instance.

XmlSchemaCollectionOwningSchema XmlSchemaCollectionOwningSchema XmlSchemaCollectionOwningSchema XmlSchemaCollectionOwningSchema

The owning relational schema where the schema collection for this XML instance is located.

Methods

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
ResetDbType() ResetDbType() ResetDbType() ResetDbType()

Resets the type associated with this SqlParameter.

ResetSqlDbType() ResetSqlDbType() ResetSqlDbType() ResetSqlDbType()

Resets the type associated with this SqlParameter.

ToString() ToString() ToString() ToString()

Gets a string that contains the ParameterName.

Explicit Interface Implementations

ICloneable.Clone() ICloneable.Clone() ICloneable.Clone() ICloneable.Clone()

For a description of this member, see Clone().

Applies to

See Also