解决Too many parameters were provi
2019-02-13 本文已影响1人
菜鸟飞不动
在使用SQL操作数据表时,如果用户定义的函数参数超过2100条会报错,
The incoming tabular data stream(TDS) remote procedure call (RPC) protocol stream is incorrect.Too many parameters provided in the RPC request.The maximum is 2100
这个原因是超过了SQL Server 的最大容量规范
指定 SQL Server 组件中定义的各种对象的最大大小和最大数量参考微软链接:https://docs.microsoft.com/zh-cn/sql/sql-server/maximum-capacity-specifications-for-sql-server?view=sql-server-2017

解决方法
既然不能超过最大容量规范,只能避免超过最大容量了,超过2100的话进行批次更新即可。
代码如下:
public void Test(IList<int> animalIds)
{
using (var db = new SqlConnection(this.connectionString))
{
db.Open();
while (animalIds.Any())
{
//一次插入1000条
var ids2Insert = animalIds.Take(1000);
animalIds = animalIds.Skip(1000).ToList();
db.Execute(SQL,
new
{
id = ids2Insert
});
}
}
}
测试:
var ids = Test(Enumerable.Range(1, 2500).ToList());