2 shuizhongjiete shuizhongjiete 于 2017.08.28 13:09 提问

SqlBulkCopy WriteToServer报超时问题

股票交易数据入库的程序,每天入库数据量约百万级。程序使用SqlBulkCopy 进行插入操作,但每天都会报一个错:
Exception:System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the opehttp://ask.csdn.net/#ration or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): 等待的操作过时。
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParserStateObject.WriteSni(Boolean canAccumulate)
at System.Data.SqlClient.TdsParserStateObject.WritePacket(Byte flushMode, Boolean canAccumulate)
at System.Data.SqlClient.TdsParserStateObject.WriteByte(Byte b)
at System.Data.SqlClient.TdsParser.WriteInt(Int32 v, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.WriteSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.WriteUnterminatedSqlValue(Object value, MetaType type, Int32 actualLength, Int32 codePageByteSize, Int32 offset, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.WriteSqlValue(Object value, MetaType type, Int32 actualLength, Int32 codePageByteSize, Int32 offset, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.WriteBulkCopyValue(Object value, SqlMetaDataPriv metadata, TdsParserStateObject stateObj, Boolean isSqlType, Boolean isDataFeed, Boolean isNull)
at System.Data.SqlClient.SqlBulkCopy.ReadWriteColumnValueAsync(Int32 col)
at System.Data.SqlClient.SqlBulkCopy.CopyColumnsAsync(Int32 col, TaskCompletionSource
1 source)
at System.Data.SqlClient.SqlBulkCopy.CopyRowsAsync(Int32 rowsSoFar, Int32 totalRows, CancellationToken cts, TaskCompletionSource1 source)
at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinued(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource
1 source)
at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsync(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource
1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestAsync(CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalAsync(CancellationToken ctoken)
at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServerAsync(Int32 columnCount, CancellationToken ctoken)
at System.Data.SqlClient.SqlBulkCopy.WriteToServer(DataTable table, DataRowState rowState)
at GTA.DSP.DTG.MDPCom.DataBaseStorage.DBOldProxy.ReflectConfig.StorageDatas(HighFreDataContext datas, DateTime storageDate)
at GTA.DSP.DTG.MDPCom.DataBaseStorage.DBOldProxy.StorageDatas(HighFreDataContext datas, DateTime storageDate)
请教高人帮忙解答下,感激不尽!
入库核心代码如下:
using (SqlConnection sqlCon = new SqlConnection(conStr))
{
sqlCon.Open();
using (SqlTransaction tran = sqlCon.BeginTransaction())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlCon, SqlBulkCopyOptions.Default, tran))
{
bulkCopy.DestinationTableName = string.Format("[{0}]", dicDataCorrInfo[detailKey].TableName);
foreach (var p in valTable.Properties)
{
bulkCopy.ColumnMappings.Add(p.ColumnName, p.ColumnName);
}
try
{
Logger.Log.DebugLog("###单批入库数量2:" + dt.Rows.Count);
bulkCopy.WriteToServer(dt);
tran.Commit();
}
catch (Exception)
{
try
{
tran.Rollback();
}
catch (Exception)
{ }
throw;
}
}
}
}

1个回答

qq_40001267
qq_40001267   2017.08.28 13:15

66666666666666666666666666666666666

Csdn user default icon
上传中...
上传图片
插入图片