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)

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:
 System::Data::SqlClient::SqlParameter ^ Add(System::Data::SqlClient::SqlParameter ^ value);
public System.Data.SqlClient.SqlParameter Add (System.Data.SqlClient.SqlParameter value);
member this.Add : System.Data.SqlClient.SqlParameter -> System.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

public void AddSqlParameter(SqlCommand command) 
{
    command.Parameters.Add(new SqlParameter("Description", "Beverages"));
}
Public Sub AddSqlParameter(command As SqlCommand) 
  command.Parameters.Add(New SqlParameter("Description", "Beverages"))
End Sub

See also

Add(Object)

Adds the specified SqlParameter object to the SqlParameterCollection.

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

Parameters

value
Object

An Object.

Returns

The index of the new SqlParameter object.

Implements

See also

Add(String, SqlDbType)

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

public:
 System::Data::SqlClient::SqlParameter ^ Add(System::String ^ parameterName, System::Data::SqlDbType sqlDbType);
public System.Data.SqlClient.SqlParameter Add (string parameterName, System.Data.SqlDbType sqlDbType);
member this.Add : string * System.Data.SqlDbType -> System.Data.SqlClient.SqlParameter

Parameters

parameterName
String

The name of the parameter.

sqlDbType
SqlDbType

One of the SqlDbType values.

Returns

A new SqlParameter object.

Examples

public void AddSqlParameter(SqlCommand command) 
{
    SqlParameter param = command.Parameters.Add(
        "@Description", SqlDbType.NVarChar);
    param.Size = 16;
    param.Value = "Beverages";
}
Public Sub AddSqlParameter(command As SqlCommand) 
  Dim param As SqlParameter = command.Parameters.Add( _
      "@Description", SqlDbType.NVarChar)
  param.Size = 16
  param.Value = "Beverages"
End Sub

See also

Add(String, Object)

Warning

This API is now obsolete.

Adds the specified SqlParameter object to the SqlParameterCollection.

public:
 System::Data::SqlClient::SqlParameter ^ Add(System::String ^ parameterName, System::Object ^ value);
[System.Obsolete("Do not call this method.")]
[System.Obsolete("Add(String parameterName, Object value) has been deprecated.  Use AddWithValue(String parameterName, Object value).  https://go.microsoft.com/fwlink/?linkid=14202", false)]
public System.Data.SqlClient.SqlParameter Add (string parameterName, object value);
override this.Add : string * obj -> System.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

public void AddSqlParameter(SqlCommand command) 
{
    SqlParameter param = new SqlParameter(
        "@Description", SqlDbType.NVarChar, 16);
    param.Value = "Beverages";
    command.Parameters.Add(param);
}
Public Sub AddSqlParameter(command As SqlCommand) 
  Dim param As New SqlParameter( _
      "@Description", SqlDbType.NVarChar, 16)
  param.Value = "Beverages"
  command.Parameters.Add(param)
End Sub

See also

Add(String, SqlDbType, Int32)

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

public:
 System::Data::SqlClient::SqlParameter ^ Add(System::String ^ parameterName, System::Data::SqlDbType sqlDbType, int size);
public System.Data.SqlClient.SqlParameter Add (string parameterName, System.Data.SqlDbType sqlDbType, int size);
member this.Add : string * System.Data.SqlDbType * int -> System.Data.SqlClient.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

public void AddSqlParameter(SqlCommand command) 
{
    SqlParameter param = new SqlParameter(
        "@Description", SqlDbType.NVarChar, 16);
    param.Value = "Beverages";
    command.Parameters.Add(param);
}
Public Sub AddSqlParameter(command As SqlCommand) 
  Dim param As New SqlParameter( _
      "@Description", SqlDbType.NVarChar, 16)
  param.Value = "Beverages"
  command.Parameters.Add(param)
End Sub

Remarks

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

See also

Add(String, SqlDbType, Int32, String)

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

public:
 System::Data::SqlClient::SqlParameter ^ Add(System::String ^ parameterName, System::Data::SqlDbType sqlDbType, int size, System::String ^ sourceColumn);
public System.Data.SqlClient.SqlParameter Add (string parameterName, System.Data.SqlDbType sqlDbType, int size, string sourceColumn);
member this.Add : string * System.Data.SqlDbType * int * string -> System.Data.SqlClient.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.

Returns

A new SqlParameter object.

Examples

public void AddSqlParameter(SqlCommand cmd) 
{
  SqlParameter p1 = cmd.Parameters.Add("@Description", SqlDbType.NVarChar, 16, "Description");
}
Public Sub AddSqlParameter(cmd As SqlCommand) 
  Dim p1 As SqlParameter = cmd.Parameters.Add("@Description", SqlDbType.NVarChar, 16, "Description")
End Sub

See also

Applies to