【发布时间】:2011-10-03 17:05:03
【问题描述】:
我有 C#-WCF/Winforms 应用程序。它使用 Oracle 客户端 API 和具有简单插入查询的存储过程在 Oracle 数据库的临时表中插入 450,000 多条记录。 在数据库中插入记录大约需要 15 分钟,有时记录也不会被插入。在 wcf 端出现各种超时错误。 有没有什么有效的方法来做这些插入?
感谢阅读。
这是我的批量插入代码:
OracleTransaction tran = null;
UpdateRowSource oldURS = this.cmd.UpdatedRowSource;
OracleCommand oldCmd = this.dbAdapter.InsertCommand;
int oldUBS = this.dbAdapter.UpdateBatchSize;
try
{
SetOutputParams();
this.OpenDBConnection();
tran = this.dbConn.BeginTransaction();
this.cmd.Transaction = tran;
this.cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
this.dbAdapter.InsertCommand = this.cmd;
this.dbAdapter.UpdateBatchSize = size;
this.dbAdapter.Update(data);
tran.Commit();
SetOutputParamValues();
}
catch (OracleException ex)
{
if (tran != null) {
tran.Rollback();
}
throw;
}
finally
{
this.CloseDBConnection();
this.cmd.Parameters.Clear();
this.cmd.UpdatedRowSource = oldURS;
this.dbAdapter.InsertCommand = oldCmd;
this.dbAdapter.UpdateBatchSize = oldUBS;
}
}
【问题讨论】:
-
在 C# 中,使用 throw 而不是 throw ex 否则你的 StackTrace 会被搞砸:stackoverflow.com/questions/178456/…
-
是的,同意 Davide。我将使用 throw;代替。谢谢。
-
这是基于 Sql Server 的。(url) 但可能会给您一些关键字来搜索“基于集合”。 docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/…我不确定oracle有这个概念。使用 sql server,我还将 xml 发送到存储过程,在那里解析(“粉碎”)xml,并进行批量插入。但 IIRC,dataadapter 的默认行为是 RBAR(逐行列)