Share via


SqlParameterCollection.Add Method

Definition

Overloads

Add(SqlParameter)

Adds the specified SqlParameter object to the SqlParameterCollection.

Add(Object)

Adds the specified SqlParameter object to the SqlParameterCollection.

Add(String, SqlDbType)

Adds a SqlParameter to the SqlParameterCollection given the parameter name and the data type.

Add(String, Object)
Obsolete.

Adds the specified SqlParameter object to the SqlParameterCollection.

Add(String, SqlDbType, Int32)

Adds a SqlParameter to the SqlParameterCollection, given the specified parameter name, SqlDbType and size.

Add(String, SqlDbType, Int32, String)

Adds a SqlParameter to the SqlParameterCollection with the parameter name, the data type, and the column length.

Add(SqlParameter)

Adds the specified SqlParameter object to the SqlParameterCollection.

public:
 Microsoft::Data::SqlClient::SqlParameter ^ Add(Microsoft::Data::SqlClient::SqlParameter ^ value);
public Microsoft.Data.SqlClient.SqlParameter Add (Microsoft.Data.SqlClient.SqlParameter value);
override this.Add : Microsoft.Data.SqlClient.SqlParameter -> Microsoft.Data.SqlClient.SqlParameter
Public Function Add (value As SqlParameter) As SqlParameter

Parameters

value
SqlParameter

The SqlParameter to add to the collection.

Returns

A new SqlParameter object.

Exceptions

The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection.

The parameter passed was not a SqlParameter.

The value parameter is null.

Examples

using Microsoft.Data.SqlClient;

public class Sample
{
    public void AddSqlParameter(SqlCommand command)
    {
        command.Parameters.Add(new SqlParameter("Description", "Beverages"));
    }
}

Applies to

Add(Object)

Adds the specified SqlParameter object to the SqlParameterCollection.

public:
 override int Add(System::Object ^ value);
public override int Add (object value);
override this.Add : obj -> int
Public Overrides Function Add (value As Object) As Integer

Parameters

value
Object

An Object.

Returns

The index of the new SqlParameter object.

Applies to

Add(String, SqlDbType)

Adds a SqlParameter to the SqlParameterCollection given the parameter name and the data type.

public:
 Microsoft::Data::SqlClient::SqlParameter ^ Add(System::String ^ parameterName, System::Data::SqlDbType sqlDbType);
public Microsoft.Data.SqlClient.SqlParameter Add (string parameterName, System.Data.SqlDbType sqlDbType);
override this.Add : string * System.Data.SqlDbType -> Microsoft.Data.SqlClient.SqlParameter
Public Function Add (parameterName As String, sqlDbType As SqlDbType) As SqlParameter

Parameters

parameterName
String

The name of the parameter.

sqlDbType
SqlDbType

One of the SqlDbType values.

Returns

A new SqlParameter object.

Examples

using Microsoft.Data.SqlClient;

public class Sample
{
    public void AddSqlParameter(SqlCommand command)
    {
        SqlParameter param = command.Parameters.Add(
            "@Description", SqlDbType.NVarChar);
        param.Size = 16;
        param.Value = "Beverages";
    }
}

Applies to

Add(String, Object)

Caution

Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value). http://go.microsoft.com/fwlink/?linkid=14202

Adds the specified SqlParameter object to the SqlParameterCollection.

public:
 Microsoft::Data::SqlClient::SqlParameter ^ Add(System::String ^ parameterName, System::Object ^ value);
[System.Obsolete("Add(String parameterName, Object value) has been deprecated.  Use AddWithValue(String parameterName, Object value).  http://go.microsoft.com/fwlink/?linkid=14202", false)]
public Microsoft.Data.SqlClient.SqlParameter Add (string parameterName, object value);
[<System.Obsolete("Add(String parameterName, Object value) has been deprecated.  Use AddWithValue(String parameterName, Object value).  http://go.microsoft.com/fwlink/?linkid=14202", false)>]
override this.Add : string * obj -> Microsoft.Data.SqlClient.SqlParameter
Public Function Add (parameterName As String, value As Object) As SqlParameter

Parameters

parameterName
String

The name of the SqlParameter to add to the collection.

value
Object

A Object.

Returns

A new SqlParameter object.

Use caution when you are using this overload of the SqlParameterCollection.Add method to specify integer parameter values. Because this overload takes a value of type Object, you must convert the integral value to an Object type when the value is zero, as the following C# example demonstrates.

parameters.Add("@pname", Convert.ToInt32(0));  

If you do not perform this conversion, the compiler assumes that you are trying to call the SqlParameterCollection.Add (string, SqlDbType) overload.

Attributes

Exceptions

The SqlParameter specified in the value parameter is already added to this or another SqlParameterCollection.

The value parameter is null.

Examples

using Microsoft.Data.SqlClient;

public class Sample
{
    public void AddSqlParameter(SqlCommand command)
    {
        SqlParameter param = new SqlParameter(
            "@Description", SqlDbType.NVarChar, 16);
        param.Value = "Beverages";
        command.Parameters.Add(param);
    }
}

Applies to

Add(String, SqlDbType, Int32)

Adds a SqlParameter to the SqlParameterCollection, given the specified parameter name, SqlDbType and size.

public:
 Microsoft::Data::SqlClient::SqlParameter ^ Add(System::String ^ parameterName, System::Data::SqlDbType sqlDbType, int size);
public Microsoft.Data.SqlClient.SqlParameter Add (string parameterName, System.Data.SqlDbType sqlDbType, int size);
override this.Add : string * System.Data.SqlDbType * int -> Microsoft.Data.SqlClient.SqlParameter
Public Function Add (parameterName As String, sqlDbType As SqlDbType, size As Integer) As SqlParameter

Parameters

parameterName
String

The name of the parameter.

sqlDbType
SqlDbType

The SqlDbType of the SqlParameter to add to the collection.

size
Int32

The size as an Int32.

Returns

A new SqlParameter object.

Examples

using Microsoft.Data.SqlClient;

public class Sample
{
    public void AddSqlParameter(SqlCommand command)
    {
        SqlParameter param = new SqlParameter(
            "@Description", SqlDbType.NVarChar, 16);
        param.Value = "Beverages";
        command.Parameters.Add(param);
    }
}

Remarks

This overload is useful when you are adding a parameter of a variable-length data type such as varchar or binary.

Applies to

Add(String, SqlDbType, Int32, String)

Adds a SqlParameter to the SqlParameterCollection with the parameter name, the data type, and the column length.

public:
 Microsoft::Data::SqlClient::SqlParameter ^ Add(System::String ^ parameterName, System::Data::SqlDbType sqlDbType, int size, System::String ^ sourceColumn);
public Microsoft.Data.SqlClient.SqlParameter Add (string parameterName, System.Data.SqlDbType sqlDbType, int size, string sourceColumn);
override this.Add : string * System.Data.SqlDbType * int * string -> Microsoft.Data.SqlClient.SqlParameter
Public Function Add (parameterName As String, sqlDbType As SqlDbType, size As Integer, sourceColumn As String) As SqlParameter

Parameters

parameterName
String

The name of the parameter.

sqlDbType
SqlDbType

One of the SqlDbType values.

size
Int32

The column length.

sourceColumn
String

The name of the source column (SourceColumn) if this SqlParameter is used in a call to Update(DataSet).

Returns

A new SqlParameter object.

Examples

using Microsoft.Data.SqlClient;

public class Sample
{
    public void AddSqlParameter(SqlCommand cmd)
    {
        SqlParameter p1 = cmd.Parameters.Add("@Description", SqlDbType.NVarChar, 16, "Description");
    }
}

Applies to