Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
SqlParameterCollection Class

Represents a collection of parameters associated with a SqlCommand and their respective mappings to columns in a DataSet. This class cannot be inherited.

System..::.Object
  System..::.MarshalByRefObject
    System.Data.Common..::.DbParameterCollection
      System.Data.SqlClient..::.SqlParameterCollection

Namespace:  System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)
Visual Basic
<ListBindableAttribute(False)> _
Public NotInheritable Class SqlParameterCollection _
    Inherits DbParameterCollection
C#
[ListBindableAttribute(false)]
public sealed class SqlParameterCollection : DbParameterCollection
Visual C++
[ListBindableAttribute(false)]
public ref class SqlParameterCollection sealed : public DbParameterCollection
F#
[<Sealed>]
[<ListBindableAttribute(false)>]
type SqlParameterCollection =  
    class
        inherit DbParameterCollection
    end

The SqlParameterCollection type exposes the following members.

  NameDescription
Public propertyCountReturns an Integer that contains the number of elements in the SqlParameterCollection. Read-only. (Overrides DbParameterCollection..::.Count.)
Public propertyIsFixedSizeGets a value that indicates whether the SqlParameterCollection has a fixed size. (Overrides DbParameterCollection..::.IsFixedSize.)
Public propertyIsReadOnlyGets a value that indicates whether the SqlParameterCollection is read-only. (Overrides DbParameterCollection..::.IsReadOnly.)
Public propertyIsSynchronizedGets a value that indicates whether the SqlParameterCollection is synchronized. (Overrides DbParameterCollection..::.IsSynchronized.)
Public propertyItem[([(Int32])])Gets the SqlParameter at the specified index.
Public propertyItem[([(String])])Gets the SqlParameter with the specified name.
Public propertySyncRootGets an object that can be used to synchronize access to the SqlParameterCollection. (Overrides DbParameterCollection..::.SyncRoot.)
Top
  NameDescription
Public methodAdd(Object)Adds the specified SqlParameter object to the SqlParameterCollection. (Overrides DbParameterCollection..::.Add(Object).)
Public methodAdd(SqlParameter)Adds the specified SqlParameter object to the SqlParameterCollection.
Public methodAdd(String, SqlDbType)Adds a SqlParameter to the SqlParameterCollection given the parameter name and the data type.
Public methodAdd(String, Object) Obsolete. Adds the specified SqlParameter object to the SqlParameterCollection.
Public methodAdd(String, SqlDbType, Int32)Adds a SqlParameter to the SqlParameterCollection, given the specified parameter name, SqlDbType and size.
Public methodAdd(String, SqlDbType, Int32, String)Adds a SqlParameter to the SqlParameterCollection with the parameter name, the data type, and the column length.
Public methodAddRange(Array)Adds an array of values to the end of the SqlParameterCollection. (Overrides DbParameterCollection..::.AddRange(Array).)
Public methodAddRange(array<SqlParameter>[]()[])Adds an array of SqlParameter values to the end of the SqlParameterCollection.
Public methodAddWithValueAdds a value to the end of the SqlParameterCollection.
Public methodClearRemoves all the SqlParameter objects from the SqlParameterCollection. (Overrides DbParameterCollection..::.Clear()()().)
Public methodContains(Object)Determines whether the specified Object is in this SqlParameterCollection. (Overrides DbParameterCollection..::.Contains(Object).)
Public methodContains(SqlParameter)Determines whether the specified SqlParameter is in this SqlParameterCollection.
Public methodContains(String)Determines whether the specified parameter name is in this SqlParameterCollection. (Overrides DbParameterCollection..::.Contains(String).)
Public methodCopyTo(Array, Int32)Copies all the elements of the current SqlParameterCollection to the specified one-dimensional Array starting at the specified destination Array index. (Overrides DbParameterCollection..::.CopyTo(Array, Int32).)
Public methodCopyTo(array<SqlParameter>[]()[], Int32)Copies all the elements of the current SqlParameterCollection to the specified SqlParameterCollection starting at the specified destination index.
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetEnumeratorReturns an enumerator that iterates through the SqlParameterCollection. (Overrides DbParameterCollection..::.GetEnumerator()()().)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodGetParameter(Int32)Returns the DbParameter object at the specified index in the collection. (Inherited from DbParameterCollection.)
Protected methodGetParameter(String)Returns DbParameter the object with the specified name. (Inherited from DbParameterCollection.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOf(Object)Gets the location of the specified Object within the collection. (Overrides DbParameterCollection..::.IndexOf(Object).)
Public methodIndexOf(SqlParameter)Gets the location of the specified SqlParameter within the collection.
Public methodIndexOf(String)Gets the location of the specified SqlParameter with the specified name. (Overrides DbParameterCollection..::.IndexOf(String).)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodInsert(Int32, SqlParameter)Inserts a SqlParameter object into the SqlParameterCollection at the specified index.
Public methodInsert(Int32, Object)Inserts an Object into the SqlParameterCollection at the specified index. (Overrides DbParameterCollection..::.Insert(Int32, Object).)
Protected methodMemberwiseClone()()()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public methodRemove(Object)Removes the specified SqlParameter from the collection. (Overrides DbParameterCollection..::.Remove(Object).)
Public methodRemove(SqlParameter)Removes the specified SqlParameter from the collection.
Public methodRemoveAt(Int32)Removes the SqlParameter from the SqlParameterCollection at the specified index. (Overrides DbParameterCollection..::.RemoveAt(Int32).)
Public methodRemoveAt(String)Removes the SqlParameter from the SqlParameterCollection at the specified parameter name. (Overrides DbParameterCollection..::.RemoveAt(String).)
Protected methodSetParameter(Int32, DbParameter)Sets the DbParameter object at the specified index to a new value. (Inherited from DbParameterCollection.)
Protected methodSetParameter(String, DbParameter)Sets the DbParameter object with the specified name to a new value. (Inherited from DbParameterCollection.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Explicit interface implemetationPrivate propertyIDataParameterCollection..::.ItemGets or sets the parameter at the specified index. (Inherited from DbParameterCollection.)
Explicit interface implemetationPrivate propertyIList..::.ItemGets or sets the element at the specified index. (Inherited from DbParameterCollection.)
Top

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 (ADO.NET).

The following example creates multiple instances of SqlParameter through the SqlParameterCollection collection. The parameters are used to select data within the data source and populate the DataSet. This code assumes that a DataSet and a SqlDataAdapter have already been created with the appropriate schema, commands, and connection.

Visual Basic
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  
C#
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);

}

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker