解决Too many parameters were provi

作者: 菜鸟飞不动 | 来源:发表于2019-02-13 14:12 被阅读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

解决方法

既然不能超过最大容量规范,只能避免超过最大容量了,超过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());

Reference:
https://stackoverflow.com/questions/39592340/how-can-i-use-more-than-2100-values-in-an-in-clause-using-dapper

相关文章

网友评论

    本文标题:解决Too many parameters were provi

    本文链接:https://www.haomeiwen.com/subject/tiireqtx.html