Прочетете на английски Редактиране

Споделяне чрез


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)

Source:
System.Data.SqlClient.notsupported.cs

Adds the specified SqlParameter object to the SqlParameterCollection.

C#
public System.Data.SqlClient.SqlParameter Add(System.Data.SqlClient.SqlParameter value);

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

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

See also

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

Add(Object)

Source:
System.Data.SqlClient.notsupported.cs

Adds the specified SqlParameter object to the SqlParameterCollection.

C#
public override int Add(object value);
C#
public int Add(object value);

Parameters

value
Object

An Object.

Returns

The index of the new SqlParameter object.

Implements

Examples

The following example demonstrates the implementation of the IList interface to create a simple, fixed-size list. This code is part of a larger example for the IList interface.

C#
class SimpleList : IList
{
    private object[] _contents = new object[8];
    private int _count;

    public SimpleList()
    {
        _count = 0;
    }

    // IList Members
    public int Add(object value)
    {
        if (_count < _contents.Length)
        {
            _contents[_count] = value;
            _count++;

            return (_count - 1);
        }

        return -1;
    }

    public void Clear()
    {
        _count = 0;
    }

    public bool Contains(object value)
    {
        for (int i = 0; i < Count; i++)
        {
            if (_contents[i] == value)
            {
                return true;
            }
        }
        return false;
    }

    public int IndexOf(object value)
    {
        for (int i = 0; i < Count; i++)
        {
            if (_contents[i] == value)
            {
                return i;
            }
        }
        return -1;
    }

    public void Insert(int index, object value)
    {
        if ((_count + 1 <= _contents.Length) && (index < Count) && (index >= 0))
        {
            _count++;

            for (int i = Count - 1; i > index; i--)
            {
                _contents[i] = _contents[i - 1];
            }
            _contents[index] = value;
        }
    }

    public bool IsFixedSize
    {
        get
        {
            return true;
        }
    }

    public bool IsReadOnly
    {
        get
        {
            return false;
        }
    }

    public void Remove(object value)
    {
        RemoveAt(IndexOf(value));
    }

    public void RemoveAt(int index)
    {
        if ((index >= 0) && (index < Count))
        {
            for (int i = index; i < Count - 1; i++)
            {
                _contents[i] = _contents[i + 1];
            }
            _count--;
        }
    }

    public object this[int index]
    {
        get
        {
            return _contents[index];
        }
        set
        {
            _contents[index] = value;
        }
    }

    // ICollection members.

    public void CopyTo(Array array, int index)
    {
        for (int i = 0; i < Count; i++)
        {
            array.SetValue(_contents[i], index++);
        }
    }

    public int Count
    {
        get
        {
            return _count;
        }
    }

    public bool IsSynchronized
    {
        get
        {
            return false;
        }
    }

    // Return the current instance since the underlying store is not
    // publicly available.
    public object SyncRoot
    {
        get
        {
            return this;
        }
    }

    // IEnumerable Members

    public IEnumerator GetEnumerator()
    {
        // Refer to the IEnumerator documentation for an example of
        // implementing an enumerator.
        throw new NotImplementedException("The method or operation is not implemented.");
    }

    public void PrintContents()
    {
        Console.WriteLine($"List has a capacity of {_contents.Length} and currently has {_count} elements.");
        Console.Write("List contents:");
        for (int i = 0; i < Count; i++)
        {
            Console.Write($" {_contents[i]}");
        }
        Console.WriteLine();
    }
}

Remarks

This member is an explicit interface member implementation. It can be used only when the SqlParameterCollection instance is cast to an IList interface.

See also

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

Add(String, SqlDbType)

Source:
System.Data.SqlClient.notsupported.cs

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

C#
public System.Data.SqlClient.SqlParameter Add(string parameterName, System.Data.SqlDbType sqlDbType);

Parameters

parameterName
String

The name of the parameter.

sqlDbType
SqlDbType

One of the SqlDbType values.

Returns

A new SqlParameter object.

Examples

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

See also

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

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.

C#
public System.Data.SqlClient.SqlParameter Add(string parameterName, object value);
C#
[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 System.Data.SqlClient.SqlParameter Add(string parameterName, object value);

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

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

See also

Applies to

.NET Framework 4.8.1 и други версии
Продукт Версии (остаряло)
.NET Framework 1.1 (2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1)

Add(String, SqlDbType, Int32)

Source:
System.Data.SqlClient.notsupported.cs

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

C#
public System.Data.SqlClient.SqlParameter Add(string parameterName, System.Data.SqlDbType sqlDbType, int size);

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

C#
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.

See also

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

Add(String, SqlDbType, Int32, String)

Source:
System.Data.SqlClient.notsupported.cs

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

C#
public System.Data.SqlClient.SqlParameter Add(string parameterName, System.Data.SqlDbType sqlDbType, int size, string sourceColumn);

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

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

See also

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)