SqlParameterCollection 类

定义

表示与 SqlCommand 关联的参数的集合以及各个参数到 DataSet 中列的映射。Represents a collection of parameters associated with a SqlCommand and their respective mappings to columns in a DataSet. 此类不能被继承。This class cannot be inherited.

public ref class SqlParameterCollection sealed : System::Data::Common::DbParameterCollection, System::Collections::IList
[System.ComponentModel.ListBindable(false)]
public sealed class SqlParameterCollection : System.Data.Common.DbParameterCollection, System.Collections.IList
type SqlParameterCollection = class
    inherit DbParameterCollection
    interface IDataParameterCollection
    interface IList
    interface ICollection
    interface IEnumerable
Public NotInheritable Class SqlParameterCollection
Inherits DbParameterCollection
Implements IList
继承
SqlParameterCollection
属性
实现

示例

下面的示例通过 SqlParameterCollection 集合创建 SqlParameter 的多个实例。The following example creates multiple instances of SqlParameter through the SqlParameterCollection collection. 参数用于选择数据源中的数据并填充 DataSetThe parameters are used to select data within the data source and populate the DataSet. 此代码假定已使用相应的架构、命令和连接创建了 DataSetSqlDataAdapterThis code assumes that a DataSet and a SqlDataAdapter have already been created with the appropriate schema, commands, and connection.

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  

注解

如果命令包含即席 SQL 语句(而不是存储过程名称),则集合中的参数数目必须等于命令文本中的参数占位符的数目,否则 SQL Server 将引发错误。If the command contains an ad hoc SQL statement, as opposed to a stored-procedure name, the number of the parameters in the collection must be equal to the number of parameter placeholders within the command text, or SQL Server raises an error. 对于存储过程,必须提供存储过程中声明的未使用默认值的所有参数。With a stored procedure, all the parameters declared in the stored procedure without a default value must be provided. 使用默认值声明的参数是可选的。Parameters declared with a default value are optional. 这允许您指定默认值以外的值。This lets you specify a value other than the default.

有关演示如何使用参数的其他示例代码的详细信息,请参阅命令和参数For more information with additional sample code demonstrating how to use parameters, see Commands and Parameters.

属性

Count

返回一个整数,包含 SqlParameterCollection 中的元素数。Returns an Integer that contains the number of elements in the SqlParameterCollection. 只读。Read-only.

IsFixedSize

获取一个值,该值指示 SqlParameterCollection 是否具有固定大小。Gets a value that indicates whether the SqlParameterCollection has a fixed size.

IsReadOnly

获取一个值,该值指示 SqlParameterCollection 是否为只读。Gets a value that indicates whether the SqlParameterCollection is read-only.

IsSynchronized

获取一个值,该值指示 SqlParameterCollection 是否已同步。Gets a value that indicates whether the SqlParameterCollection is synchronized.

Item[Int32]

获取位于指定索引处的 SqlParameterGets the SqlParameter at the specified index.

Item[String]

获取具有指定名称的 SqlParameterGets the SqlParameter with the specified name.

SyncRoot

获取可用于同步对 SqlParameterCollection 的访问的对象。Gets an object that can be used to synchronize access to the SqlParameterCollection.

方法

Add(Object)

将指定的 SqlParameter 对象添加到 SqlParameterCollection 中。Adds the specified SqlParameter object to the SqlParameterCollection.

Add(SqlParameter)

将指定的 SqlParameter 对象添加到 SqlParameterCollection 中。Adds the specified SqlParameter object to the SqlParameterCollection.

Add(String, Object)

将指定的 SqlParameter 对象添加到 SqlParameterCollection 中。Adds the specified SqlParameter object to the SqlParameterCollection.

Add(String, SqlDbType)

在给定参数名称和数据类型时向 SqlParameter 中添加一个 SqlParameterCollectionAdds a SqlParameter to the SqlParameterCollection given the parameter name and the data type.

Add(String, SqlDbType, Int32)

SqlParameter 添加到 SqlParameterCollection 中(给定了指定的参数名、SqlDbType 和大小)。Adds a SqlParameter to the SqlParameterCollection, given the specified parameter name, SqlDbType and size.

Add(String, SqlDbType, Int32, String)

使用给定的参数名称、数据类型和列长度向 SqlParameter 中添加一个 SqlParameterCollectionAdds a SqlParameter to the SqlParameterCollection with the parameter name, the data type, and the column length.

AddRange(Array)

SqlParameterCollection 的末尾添加一个值数组。Adds an array of values to the end of the SqlParameterCollection.

AddRange(SqlParameter[])

SqlParameter 的末尾添加一个 SqlParameterCollection 值数组。Adds an array of SqlParameter values to the end of the SqlParameterCollection.

AddWithValue(String, Object)

SqlParameterCollection 的末尾添加一个值。Adds a value to the end of the SqlParameterCollection.

Clear()

SqlParameter 中移除所有的 SqlParameterCollection 对象。Removes all the SqlParameter objects from the SqlParameterCollection.

Contains(Object)

确定指定的 Object 是否在此 SqlParameterCollection 中。Determines whether the specified Object is in this SqlParameterCollection.

Contains(SqlParameter)

确定指定的 SqlParameter 是否在此 SqlParameterCollection 中。Determines whether the specified SqlParameter is in this SqlParameterCollection.

Contains(String)

确定指定的参数名是否在SqlParameterCollectionDetermines whether the specified parameter name is in this SqlParameterCollection.

CopyTo(Array, Int32)

将当前 SqlParameterCollection 的所有元素复制到指定的一维 Array 中,并以指定的目标 Array 索引位置作为复制到的起始位置。Copies all the elements of the current SqlParameterCollection to the specified one-dimensional Array starting at the specified destination Array index.

CopyTo(SqlParameter[], Int32)

将当前 SqlParameterCollection 的所有元素复制到指定的 SqlParameterCollection 中,并以指定的目标索引位置作为复制到的起始位置。Copies all the elements of the current SqlParameterCollection to the specified SqlParameterCollection starting at the specified destination index.

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetEnumerator()

返回循环访问 SqlParameterCollection 的枚举数。Returns an enumerator that iterates through the SqlParameterCollection.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IndexOf(Object)

获取指定的 Object 在集合中的位置。Gets the location of the specified Object within the collection.

IndexOf(SqlParameter)

获取指定的 SqlParameter 在集合中的位置。Gets the location of the specified SqlParameter within the collection.

IndexOf(String)

获取具有指定名称的指定 SqlParameter 的位置。Gets the location of the specified SqlParameter with the specified name.

InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
Insert(Int32, Object)

Object 中的指定索引处插入一个 SqlParameterCollectionInserts an Object into the SqlParameterCollection at the specified index.

Insert(Int32, SqlParameter)

SqlParameter 中的指定索引处插入一个 SqlParameterCollection 对象。Inserts a SqlParameter object into the SqlParameterCollection at the specified index.

MemberwiseClone()

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

(继承自 Object)
MemberwiseClone(Boolean)

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

(继承自 MarshalByRefObject)
Remove(Object)

从集合中移除指定的 SqlParameterRemoves the specified SqlParameter from the collection.

Remove(SqlParameter)

从集合中移除指定的 SqlParameterRemoves the specified SqlParameter from the collection.

RemoveAt(Int32)

SqlParameter 的指定索引处移除 SqlParameterCollectionRemoves the SqlParameter from the SqlParameterCollection at the specified index.

RemoveAt(String)

SqlParameter 中移除具有指定参数名称的 SqlParameterCollectionRemoves the SqlParameter from the SqlParameterCollection at the specified parameter name.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅