另一个SqlParameterCollection中已包含SqlParameter

一般情况下,我们定义的一个SqlParameter参数数组,如:

            SqlParameter[] parms = 
            {
                new SqlParameter("@Date", date),
                new SqlParameter("@Time", time)
            };

如果只给一个SqlCommand使用,这种情况的参数使用,不会出现异常,但如果该参数数组同时给两个Sqlcommand使用,就会出现如下异常:
  System.ArgumentException: 另一个SqlParameterCollection中已包含SqlParameter。
  
原因如下:
声明的SqlParameter数组,而在循环的内部,每一次执行ExecuteNonQuery(或者其它命令方法)都由该方法内部的IDbCommand.Parameters.Add(IDbDataParameter)将SqlParameter数组添加到IDbCommand的IDataParameterCollection中。而framework机制限制两个IDataParameterCollection指向同一个对象。虽然ExecuteNonQuery方法内部声明了一个IDbCommand的临时对象,理论上讲,这个包含了IDataParameterCollection的IDbCommand对象会在ExecuteNonQuery方法结束时从内存中释放。但是实际上可能是由于垃圾回收机制并没有将IDbCommand临时对象即时的回收,而且改对象绑定的Parameter集合也存在,就像一个DropDownList添加Item一样。这样在下一个循环执行的时候,会导致两个IDataParameterCollection指向同一个对象,此时出现问题。

解决方案一:在每一次循环时,重新生成对象,但这样会产生大量的垃圾变量,不可取。

解决方案二:将使用完之后的Command命令的Parameters集合清空。推荐使用,类似代码如下:

        /// <summary>
        /// 获取一个DataTable
        /// </summary>
        public static DataTable GetDataTable(
            string connDBStr, string sql, params SqlParameter[] cmdParms)
        {
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(connDBStr))
            {
                PrepareSqlCommand(cmd, conn, null, sql, cmdParms);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable    (SetSqlAsDataTableName(sql));
                da.Fill(dt);
                cmd.Parameters.Clear();//多了这一句,就解决了问题
                return dt;
            }
        }

另外,如果不是数组,只是一个SqlParameter变量,如:

            SqlParameter parm = 
                new SqlParameter("@Cust_Id", CustId.Trim());;

则多次被SqlCommand使用,不会出现问题,我已经做了试验!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 26
    评论
这个异常通常是由于使用了错误的参数类型导致的。根据你提供的异常信息,看起来是在使用 `SqlParameter` 时出现了问题。 在使用 `ExecuteSqlRaw` 或 `ExecuteSqlInterpolated` 方法时,应该使用 `Microsoft.Data.SqlClient.SqlParameter` 类型而不是 `System.Data.SqlClient.SqlParameter` 类型。 请确保在代码引用了正确的命名空间,并使用 `Microsoft.Data.SqlClient.SqlParameter` 类型来创建参数对象。 以下是一个修正该异常的示例代码: ```csharp using Microsoft.EntityFrameworkCore; using Microsoft.Data.SqlClient; // 确保引用了正确的命名空间 // 创建 DbContext 类 public class YourDbContext : DbContext { public YourDbContext(DbContextOptions<YourDbContext> options) : base(options) { } // DbSet 和其他属性... public DbSet<IPS_Invoice> IPS_Invoices { get; set; } } public class IPS_Invoice { public int IPS_ID { get; set; } public bool BLOCK { get; set; } public DateTime? BLOCKTIME { get; set; } } public class YourRepository { private readonly YourDbContext _dbContext; public YourRepository(YourDbContext dbContext) { _dbContext = dbContext; } public string UpdateBlockTime(IPS_Invoice model) { string message = ""; try { int affectedRows = _dbContext.Database.ExecuteSqlInterpolated($"UPDATE IPS_Invoices SET BLOCK = true, BLOCKTIME = {DateTime.Now} WHERE IPS_ID = {model.IPS_ID}"); if (affectedRows > 0) { message = "True"; } else { message = "No records updated"; } } catch (Exception e) { message = "False"; // 处理异常... } return message; } } ``` 请确保在代码引用了正确的 `Microsoft.Data.SqlClient` 命名空间,并使用 `Microsoft.Data.SqlClient.SqlParameter` 类型来创建参数对象。这样应该可以解决该异常。 如果问题仍然存在,请提供更多相关的代码和异常堆栈信息,以便我能够更好地帮助你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值