我有一个控制台应用程序,它比较来自两个不同数据库的两个表,并从另一个表中返回缺少的数据。我有此错误消息:"System.Data中发生了'System.Data.SqlClient.SqlException'类型的未处理异常.dll
其他信息:传入表格数据流 (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]);
}
}
}
}
请考虑使用简单的批处理机制,下面是适用于 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();
}
。这将返回参数的"批处理",然后您可以使用这些参数来运行多个查询......
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]);
}
});
}
}
}