解决:另一个 SqlParameterCollection 中已包含 SqlParameter。

执行下列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();

以上。

这个异常通常是由于使用了错误的参数类型导致的。根据你提供的异常信息,看起来是在使用 `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` 类型来创建参数对象。这样应该可以解决该异常。 如果问题仍然存在,请提供更多相关的代码和异常堆栈信息,以便我能够更好地帮助你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雕技小虫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值