SqlParameterCollection Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents a collection of parameters associated with a SqlCommand and their respective mappings to columns in a DataSet. This class cannot be inherited.
For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient.
public ref class SqlParameterCollection sealed : System::Data::Common::DbParameterCollection
public ref class SqlParameterCollection sealed : MarshalByRefObject, System::Collections::IList, System::Data::IDataParameterCollection
public sealed class SqlParameterCollection : System.Data.Common.DbParameterCollection
[System.ComponentModel.ListBindable(false)]
public sealed class SqlParameterCollection : MarshalByRefObject, System.Collections.IList, System.Data.IDataParameterCollection
[System.ComponentModel.ListBindable(false)]
public sealed class SqlParameterCollection : System.Data.Common.DbParameterCollection
type SqlParameterCollection = class
inherit DbParameterCollection
[<System.ComponentModel.ListBindable(false)>]
type SqlParameterCollection = class
inherit MarshalByRefObject
interface IDataParameterCollection
interface IList
interface ICollection
interface IEnumerable
[<System.ComponentModel.ListBindable(false)>]
type SqlParameterCollection = class
inherit DbParameterCollection
Public NotInheritable Class SqlParameterCollection
Inherits DbParameterCollection
Public NotInheritable Class SqlParameterCollection
Inherits MarshalByRefObject
Implements IDataParameterCollection, IList
- Inheritance
- Inheritance
- Inheritance
- Attributes
- Implements
Examples
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.
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
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.
Properties
| Name | Description |
|---|---|
| Count |
Returns an Integer that contains the number of elements in the SqlParameterCollection. Read-only. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| IsFixedSize |
Gets a value that indicates whether the SqlParameterCollection has a fixed size. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| IsReadOnly |
Gets a value that indicates whether the SqlParameterCollection is read-only. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| IsSynchronized |
Gets a value that indicates whether the SqlParameterCollection is synchronized. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Item[Int32] |
Gets the SqlParameter at the specified index. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Item[String] |
Gets the SqlParameter with the specified name. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| SyncRoot |
Gets an object that can be used to synchronize access to the SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
Methods
| Name | Description |
|---|---|
| Add(Object) |
Adds the specified SqlParameter object to the SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Add(SqlParameter) |
Adds the specified SqlParameter object to the SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Add(String, Object) |
Obsolete.
Adds the specified SqlParameter object to the SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Add(String, SqlDbType, Int32, String) |
Adds a SqlParameter to the SqlParameterCollection with the parameter name, the data type, and the column length. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Add(String, SqlDbType, Int32) |
Adds a SqlParameter to the SqlParameterCollection, given the specified parameter name, SqlDbType and size. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Add(String, SqlDbType) |
Adds a SqlParameter to the SqlParameterCollection given the parameter name and the data type. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| AddRange(Array) |
Adds an array of values to the end of the SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| AddRange(SqlParameter[]) |
Adds an array of SqlParameter values to the end of the SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| AddWithValue(String, Object) |
Adds a value to the end of the SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Clear() |
Removes all the SqlParameter objects from the SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Contains(Object) |
Determines whether the specified Object is in this SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Contains(SqlParameter) |
Determines whether the specified SqlParameter is in this SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Contains(String) |
Determines whether the specified parameter name is in this SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| CopyTo(Array, Int32) |
Copies all the elements of the current SqlParameterCollection to the specified one-dimensional Array starting at the specified destination Array index. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| CopyTo(SqlParameter[], Int32) |
Copies all the elements of the current SqlParameterCollection to the specified SqlParameterCollection starting at the specified destination index. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| 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) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
| GetEnumerator() |
Returns an enumerator that iterates through the SqlParameterCollection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
| GetLifetimeService() |
Obsolete.
Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject) |
| GetParameter(Int32) |
Returns the DbParameter object at the specified index in the collection. (Inherited from DbParameterCollection) |
| GetParameter(String) |
Returns DbParameter the object with the specified name. (Inherited from DbParameterCollection) |
| GetType() |
Gets the Type of the current instance. (Inherited from Object) |
| IndexOf(Object) |
Gets the location of the specified Object within the collection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| IndexOf(SqlParameter) |
Gets the location of the specified SqlParameter within the collection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| IndexOf(String) |
Gets the location of the specified SqlParameter with the specified name. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| InitializeLifetimeService() |
Obsolete.
Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject) |
| Insert(Int32, Object) |
Inserts an Object into the SqlParameterCollection at the specified index. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Insert(Int32, SqlParameter) |
Inserts a SqlParameter object into the SqlParameterCollection at the specified index. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
| MemberwiseClone(Boolean) |
Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject) |
| Remove(Object) |
Removes the specified SqlParameter from the collection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| Remove(SqlParameter) |
Removes the specified SqlParameter from the collection. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| RemoveAt(Int32) |
Removes the SqlParameter from the SqlParameterCollection at the specified index. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| RemoveAt(String) |
Removes the SqlParameter from the SqlParameterCollection at the specified parameter name. For new development, use Microsoft.Data.SqlClient. This page documents System.Data.SqlClient. |
| SetParameter(Int32, DbParameter) |
Sets the DbParameter object at the specified index to a new value. (Inherited from DbParameterCollection) |
| SetParameter(String, DbParameter) |
Sets the DbParameter object with the specified name to a new value. (Inherited from DbParameterCollection) |
| ToString() |
Returns a string that represents the current object. (Inherited from Object) |
Explicit Interface Implementations
| Name | Description |
|---|---|
| ICollection.IsSynchronized |
Gets a value indicating whether access to the ICollection is synchronized (thread safe). (Inherited from DbParameterCollection) |
| IDataParameterCollection.Item[String] |
Gets or sets the parameter at the specified index. (Inherited from DbParameterCollection) |
| IList.IsFixedSize |
Gets a value indicating whether the IList has a fixed size. (Inherited from DbParameterCollection) |
| IList.IsReadOnly |
Gets a value indicating whether the IList is read-only. (Inherited from DbParameterCollection) |
| IList.Item[Int32] |
Gets or sets the element at the specified index. (Inherited from DbParameterCollection) |
Extension Methods
| Name | Description |
|---|---|
| AsParallel(IEnumerable) |
Enables parallelization of a query. |
| AsQueryable(IEnumerable) |
Converts an IEnumerable to an IQueryable. |
| Cast<TResult>(IEnumerable) |
Casts the elements of an IEnumerable to the specified type. |
| OfType<TResult>(IEnumerable) |
Filters the elements of an IEnumerable based on a specified type. |