执行下列SQL命令语句:
SqlParameter[] parameters ……
SqlCommand command ……
command .Parameters.AddRange(parameters);
command .ExecuteScalar();
SqlCommand command2 ……
command2 .Parameters.AddRange(parameters);
command2.ExecuteNonQuery();
报错:
The SqlParameter is already contained by another SqlParameterCollection.
另一个 SqlParameterCollection 中已包含 SqlParameter。
尝试解决:
添加下列语句,仍然报错。
SqlParameter[] parameters ……
SqlCommand command ……
command .Parameters.AddRange(parameters);
command .ExecuteScalar();
command.Parameters.Clear();
SqlCommand command2 ……
command2 .Parameters.AddRange(parameters);
command2.ExecuteNonQuery();
解决:
SqlParameter[] parameters ……
SqlCommand command ……
command .Parameters.AddRange(parameters);
command .ExecuteScalar();
SqlCommand command2 ……
command2 .Parameters.AddRange(parameters.Select(x => x.Clone()).ToArray()));
command2.ExecuteNonQuery();
以上。