SqlParameterCollection.Add SqlParameterCollection.Add SqlParameterCollection.Add SqlParameterCollection.Add Method

Definition

Overloads

Add(SqlParameter) Add(SqlParameter) Add(SqlParameter) Add(SqlParameter)

Adds the specified SqlParameter object to the SqlParameterCollection.

Add(Object) Add(Object) Add(Object) Add(Object)

Adds the specified SqlParameter object to the SqlParameterCollection.

Add(String, SqlDbType) Add(String, SqlDbType) Add(String, SqlDbType)

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

Add(String, Object) Add(String, Object) Add(String, Object) Add(String, Object)

Adds the specified SqlParameter object to the SqlParameterCollection.

Add(String, SqlDbType, Int32) Add(String, SqlDbType, Int32) Add(String, SqlDbType, Int32)

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

Add(String, SqlDbType, Int32, String) Add(String, SqlDbType, Int32, String) Add(String, SqlDbType, Int32, String)

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

Add(SqlParameter) Add(SqlParameter) Add(SqlParameter) 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);
override this.Add : System.Data.SqlClient.SqlParameter -> System.Data.SqlClient.SqlParameter
Public Function Add (value As SqlParameter) As SqlParameter
Parameters
value
SqlParameter SqlParameter SqlParameter SqlParameter

The SqlParameter to add to the collection.

Returns
Exceptions

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

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

Add(Object) Add(Object) Add(Object) 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
Returns

The index of the new SqlParameter object.

Implements

Add(String, SqlDbType) Add(String, SqlDbType) 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);
override this.Add : string * System.Data.SqlDbType -> System.Data.SqlClient.SqlParameter
Parameters
parameterName
String String String String

The name of the parameter.

sqlDbType
SqlDbType SqlDbType SqlDbType SqlDbType

One of the SqlDbType values.

Returns

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

Add(String, Object) Add(String, Object) Add(String, Object) 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.")]
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 String String String

The name of the SqlParameter to add to the collection.

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.

Exceptions

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

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 SqlParameter = New SqlParameter( _
      "@Description", SqlDbType.NVarChar, 16)
  param.Value = "Beverages"
  command.Parameters.Add(param)
End Sub

Add(String, SqlDbType, Int32) Add(String, SqlDbType, Int32) 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);
override this.Add : string * System.Data.SqlDbType * int -> System.Data.SqlClient.SqlParameter
Parameters
parameterName
String String String String

The name of the parameter.

sqlDbType
SqlDbType SqlDbType SqlDbType SqlDbType

The SqlDbType of the SqlParameter to add to the collection.

size
Int32 Int32 Int32 Int32

The size as an Int32.

Returns

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 SqlParameter = 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.

Add(String, SqlDbType, Int32, String) Add(String, SqlDbType, Int32, String) 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);
override this.Add : string * System.Data.SqlDbType * int * string -> System.Data.SqlClient.SqlParameter
Parameters
parameterName
String String String String

The name of the parameter.

sqlDbType
SqlDbType SqlDbType SqlDbType SqlDbType

One of the SqlDbType values.

size
Int32 Int32 Int32 Int32

The column length.

sourceColumn
String String String String

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

Returns

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

Applies to