SqlParameter SqlParameter SqlParameter SqlParameter Class

定义

表示 SqlCommand 的参数,或者其与 DataSet 列的映射。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
继承
属性
实现

示例

下面的示例创建的多个实例SqlParameter通过SqlParameterCollection内的集合, SqlDataAdapterThe following example creates multiple instances of SqlParameter through the SqlParameterCollection collection within the SqlDataAdapter. 这些参数用于从数据源选择数据并将数据放入DataSetThese parameters are used to select data from the data source and put the data in the DataSet. 此示例假定DataSet和一个SqlDataAdapter已创建使用适当的架构、 命令和连接。This example assumes that a DataSet and a SqlDataAdapter have already been created by using the appropriate schema, commands, and connection. 有关详细信息和使用参数的其他示例,请参阅检索和修改 ADO.NET 中的数据配置参数和参数数据类型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  

注解

参数名称不区分大小写。Parameter names are not case sensitive.

备注

无名称,也称为序号、 参数不支持.NET Framework 数据提供程序适用于 SQL Server。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.

构造函数

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

初始化 SqlParameter 类的新实例。Initializes a new instance of the SqlParameter class.

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

初始化 SqlParameter 类的新实例,该类使用参数名称和新 SqlParameter 的值。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)

使用提供的参数名称和数据类型初始化 SqlParameter 类的新实例。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)

使用参数名称、SqlDbType 和大小初始化 SqlParameter 类的新实例。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)

使用提供的参数名称、参数类型、参数大小、ParameterDirection、参数精度、参数小数位数、源列、要使用的 DataRowVersion 以及参数值初始化 SqlParameter 类的新实例。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, String) SqlParameter(String, SqlDbType, Int32, String) SqlParameter(String, SqlDbType, Int32, String) SqlParameter(String, SqlDbType, Int32, String)

使用提供的参数名称、SqlDbType、大小和源列名初始化 SqlParameter 类的新实例。Initializes a new instance of the SqlParameter class that uses the parameter name, the SqlDbType, the size, and the source column name.

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

使用以下信息初始化 SqlParameter 类的新实例:参数名、参数类型、参数长度、参数传递方向、参数精度、参数的范围、源列名、DataRowVersion 的一个值、源列映射的布尔值、SqlParameter 的值、此 XML 实例的架构集所在的数据库名称、此 XML 实例的架构集所在的关系型架构,以及此参数的架构集的名称。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.

属性

CompareInfo 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

获取或设置参数的 SqlDbTypeGets 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

当使用 Always Encrypted 时强制执行参数的加密。Enforces encryption of a parameter when using Always Encrypted. 如果 SQL Server 通知驱动程序参数不需要进行加密,则使用该参数的查询会失败。If SQL Server informs the driver that the parameter does not need to be encrypted, the query using the parameter will fail. 此属性针对安全攻击提供附加保护,这些安全攻击涉及向客户端提供不正确的加密元数据的受威胁的 SQL Server,可能导致数据泄露。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

获取或设置一个值,该值指示参数是否接受 null 值。Gets or sets a value that indicates whether the parameter accepts null values. IsNullable 不用于验证参数的值,并且在执行命令时不会阻止发送或接收 null 值。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

获取或设置 Value 属性的偏移量。Gets or sets the offset to the Value property.

ParameterName ParameterName ParameterName ParameterName

获取或设置 SqlParameter 的名称。Gets or sets the name of the SqlParameter.

Precision Precision Precision Precision

获取或设置用于表示 Value 属性的最大位数。Gets or sets the maximum number of digits used to represent the Value property.

Scale Scale Scale Scale

获取或设置所解析的 Value 的小数位数。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

获取或设置映射到 DataSet 并且用于加载或返回 Value 的源列的名称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

设置或获取一个值,该值指示源列是否可以为 null。Sets or gets a value which indicates whether the source column is nullable. 通过此操作,SqlCommandBuilder 能够为可以为 null 的列正确地生成 Update 语句。This allows SqlCommandBuilder to correctly generate Update statements for nullable columns.

SourceVersion SourceVersion SourceVersion SourceVersion

获取或设置要在加载 DataRowVersion 时使用的 ValueGets or sets the DataRowVersion to use when you load Value

SqlDbType SqlDbType SqlDbType SqlDbType

获取或设置参数的 SqlDbTypeGets or sets the SqlDbType of the parameter.

SqlValue SqlValue SqlValue SqlValue

获取或设置作为 SQL 类型的参数的值。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

获取或设置作为参数的表示用户定义类型的 stringGets 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

获取此 XML 实例的架构集合所在的数据库的名称。Gets the name of the database where the schema collection for this XML instance is located.

XmlSchemaCollectionName XmlSchemaCollectionName XmlSchemaCollectionName XmlSchemaCollectionName

获取此 XML 实例的架构集合的名称。Gets the name of the schema collection for this XML instance.

XmlSchemaCollectionOwningSchema XmlSchemaCollectionOwningSchema XmlSchemaCollectionOwningSchema XmlSchemaCollectionOwningSchema

此 XML 实例的架构集合所在的所属关系架构。The owning relational schema where the schema collection for this XML instance is located.

方法

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()

获取当前实例的 TypeGets 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()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

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

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

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

重置与此 SqlParameter 关联的类型。Resets the type associated with this SqlParameter.

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

重置与此 SqlParameter 关联的类型。Resets the type associated with this SqlParameter.

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

获取一个包含 ParameterName 的字符串。Gets a string that contains the ParameterName.

显式界面实现

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

有关此成员的说明,请参见 Clone()For a description of this member, see Clone().

适用于

另请参阅