简体   繁体   English

具有超过2000个参数的ADO.NET批量插入

[英]ADO.NET Batch Insert with over 2000 parameters

I'm using Enterprise library, but the idea is the same. 我正在使用企业库,但想法是一样的。 I have a SqlStringCommand and the sql 我有一个SqlStringCommand和sql

is constructed using StringBuilder in the forms of 是使用StringBuilder以形式构造的

"insert into table (column1, column2, column3) values (@param1-X, @param2-X, @parm3-X)"+" " 

where "X" represents a "for loop" about 700 rows 其中“X”表示约700行的“for循环”

StringBuilder sb = new StringBuilder();
for(int i=0; i<700; i++)
{
   sb.Append("insert into table (column1, column2, column3) values (@param1-"+i+", @param2-"+i, +",@parm3-"+i+") " );
}

followed by constructing a command object injecting all the parameters w/ values into it. 然后构造一个命令对象,将所有带有值的参数注入其中。

Essentially, 700 rows with 3 parameters, I ended up with 2100 parameters for this "one sql" Statement. 基本上,700行有3个参数,我最终得到2100个参数用于这个“one sql”语句。

It ran fine for about a few days and suddenly I got this error 它跑了好几天,突然间我得到了这个错误

===============================================================

A severe error occurred on the current command. The results, if any, should be discarded.

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) 
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) 
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 
at System.Data.SqlClient.SqlCommand.InternalExecuteNon

Any pointers are greatly appreciated. 任何指针都非常感谢。

Have a look at SQLBulkCopy . 看看SQLBulkCopy It will probably offer a better solution to your problem than your current approach. 与您当前的方法相比,它可能会为您的问题提供更好的解决方案。

I think you can resolve this one of many ways 我想你可以通过多种方式解决这个问题

  1. Batch inserts for every 100 records 每100条记录批量插入

  2. Do each insert as it's own command with a transaction wrapped around all inserts. 每个插入都是它自己的命令,并且事务处理所有插入。

  3. Use SQL Bulk Copy 使用SQL批量复制

  4. Use SSIS for this: ETL tasks are best done using ETL tools. 使用SSIS:ETL任务最好使用ETL工具完成。 Since you are using SQL Server, you can easily load up data files into SQL Server using SSIS. 由于您使用的是SQL Server,因此可以使用SSIS轻松地将数据文件加载到SQL Server中。 You can build an SSIS package and execute it from within your C# code. 您可以构建一个SSIS包并在C#代码中执行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM