作为年度流程的一部分,需要向数据库插入数百万条记录。 我使用SqlBulkCopy.WriteToServer方法来执行此操作。
当处理下面遇到异常的较少数量的记录时。
System.Data.SqlClient.SqlException(0x80131904):严重错误 发生在当前命令上。结果,如果有的话,应该是 丢弃。当前命令发生严重错误。该 结果,如果有的话,应该被丢弃。声明一直如此 终止。在 System.Data.SqlClient.SqlConnection.OnError(SqlException异常, 布尔值breakConnection,Action`1 wrapCloseInAction)at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,布尔& dataReady)at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject state.Data.SqlClient.SqlBulkCopy.RunParser(Bulk
)中的stateObj)
以下是相关代码:
utlConnection utlCoreDBConnection = HelperFunction.GetDBConnectionProperties("core");
string astrCoreDBConnection = utlCoreDBConnection.istrConnectionString;
SqlBulkCopy lSqlBulCopy = new SqlBulkCopy(astrCoreDBConnection);
lSqlBulCopy.BulkCopyTimeout = 0;
lSqlBulCopy.WriteToServer(ldtbActuarialActiveDetail);
来自appsettings的Connectionstring:
<connection ID="core" QueryType="SqlServerClient">
<dbstring value="Data Source=my-server;Initial Catalog=my-db;User ID=user;password=devpwd;TimeOut=1044;Persist Security Info=True;Asynchronous Processing=True"/>
<dbfactory value="System.Data.SqlClient" />
<dbnextquery value="select next value for sequence_name from sgs_system_management" />
<dbprevquery value="select previous value for sequence_name from sgs_system_management" />
<dbkeyquery value="select ident_current('name_of_the_table')" />
</connection>
有人可以帮我解决这个例外吗?