此RPC请求中提供的参数太多

时间:2017-05-18 12:23:54

标签: c# linq linq-to-sql compare console-application

我有一个控制台应用程序,它比较来自两个不同数据库的两个表,并从另一个表中返回丢失的数据。我有此错误消息:“System.Data.dll中发生了'System.Data.SqlClient.SqlException'类型的未处理异常

其他信息:传入的表格数据流(TDS)远程过程调用(RPC)协议流不正确。此RPC请求中提供的参数太多。最高为2100。“

我知道,有太多的参数(6400),但我应该如何比较这两个表并获取缺失的数据?我试过这段代码:

static void Main(string[] args)
{
    using (DataClasses_LOG01DataContext log01 = new DataClasses_LOG01DataContext(ConfigurationManager.ConnectionStrings["conn_log"].ConnectionString))
    {
        List<string> list = new List<string>();

        var l01 = log01.name_numbers
            .Select(x => new { x.name, x.number });

        foreach (var item in l01)
        {
            list.Add(item.name.Replace(".", "") + "_" + item.number);
        }

        using (DataClasses_SQL01DataContext sql01 = new DataClasses_SQL01DataContext(ConfigurationManager.ConnectionStrings["conn_sql"].ConnectionString))
        {
            var log = sql01.names
                .Select(x => new { x.name });

            var missing = log.Where(x => !list.Contains(x.name));

            foreach (var item in missing)
            {
                string[] result = item.ToString().Split('_');

                Console.WriteLine("{0} {1}", result[0], result[1]);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

考虑使用简单的批处理机制,这是一个简单的LINQ扩展方法,可能有所帮助...

public static IQueryable<List<T>> BatchesOf<T>(this IQueryable<T> source, int chunkSize)
{
    return source
        .Select((x, i) => new { Index = i, Value = x })
        .GroupBy(x => x.Index / chunkSize)
        .Select(x => x.Select(v => v.Value).ToList())
        .ToList();
}

...这将返回&#34;批次&#34;您可以用来运行多个查询的params ...

static void Main(string[] args)
{
    using (DataClasses_LOG01DataContext log01 = new DataClasses_LOG01DataContext(ConfigurationManager.ConnectionStrings["conn_log"].ConnectionString))
    {
        var list = log01.name_numbers
            .Select(x => x.name.Replace(".", "") + "_" + x.number))
            .BatchesOf(1000)
            .ToList();

        using (DataClasses_SQL01DataContext sql01 = new DataClasses_SQL01DataContext(ConfigurationManager.ConnectionStrings["conn_sql"].ConnectionString))
        {
            list.ForEach(batch => {
               var log = sql01.names.Select(x => x.name);
               var missing = log.Where(x => !batch.Contains(x.name));

               foreach (var item in missing)
               {
                  string[] result = item.ToString().Split('_');
                  Console.WriteLine("{0} {1}", result[0], result[1]);
               }
            });

        }
    }
}