1

The SqlParameterCollection has an Add(SqlParameter) method, and AddRange(SqlParameter[]) method. Each has several overloads.

Specifically, SqlCommand.Parameters is read only, and if I have a SqlParameterCollection I can't just assign it, so I want to add it onto the existing collection.

Right now there's a loop in my code:

SqlCommand cmd = new SqlCommand();
foreach (SqlParameter param in collection)
{
    cmd.Parameters.Add(param);
}

Is there an easy built-in way to add a SqlParameterCollection to another?

4

1 回答 1

0

我设置了一个扩展方法以使过程更容易。但是,还没有找到“真正的”方法:

(编辑来自@DanielPaoliello 的重新建议)

public static void AddCollection(this SqlParameterCollection basic, SqlParameterCollection collection)
{
    foreach (SqlParameter param in collection)
    {
        basic.Add(((param as ICloneable).Clone()) as SqlParameter);
    }
}

比用作:

cmd.Parameters.AddCollection(collection);
于 2013-05-06T08:01:56.970 回答