我用C#-WCF/Winforms在application.It数据库的临时表中插入了450,000多条记录,这是使用Oracle Client API和一个具有简单插入查询的存储过程实现的。在db中插入记录大约需要15分钟,有时在wcf端会出现各种超时错误( too..giving ),导致记录不会被插入。有没有什么有效的方法来做这些插入?
感谢您的阅读。
下面是我的代码,它执行批量插入:
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;
} }
发布于 2011-10-04 22:41:08
将数据加载到表中的最快方法是datapump ( impdp实用程序)。另一种快速方法是SQL*Loader。
如果您想继续使用C#,请查看批量操作。我的google karma找到了the following examples
发布于 2012-06-13 18:41:27
我经常使用C#中的SQL*Loader将记录批量加载到stage。
代码的要点是这样的:
public string RunSqlLdr(string user, string password, string dsn, string fileNameCtl, string fileNameLog)
{
// Redirect both streams so we can write/read them.
var cmdProcessInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false
};
// Start the process.
var process = System.Diagnostics.Process.Start(cmdProcessInfo);
// Issue the sqlldr command and exit.
process.StandardInput.WriteLine("cd " + _directoryPath);
process.StandardInput.WriteLine("sqlldr " + user + "/" + password + "@" + dsn + " control=" + fileNameCtl + " log=" + fileNameLog);
process.StandardInput.WriteLine("exit");
// Read all the output generated from it.
var output = process.StandardOutput.ReadToEnd();
process.Dispose();
return output;
}这将从命令行返回输出,但您还需要检查生成的日志文件中加载的记录和错误计数等。
发布于 2012-09-05 01:03:57
我将大量数据插入到位于澳大利亚的Oracle数据库中,该数据库远离我在C#中运行客户端应用程序的位置。
以下是我是如何做到这一点的总结。使用数组绑定,我可以如此快速地插入数十万条记录,这让我感到惊讶。
这不是确切的代码,但您可以理解其中的含义:
using System.Data.OleDb;
int numRecords = 2;
int[] DISTRIBNO = new int[numRecords];
DISTRIBNO[0] = 100;
DISTRIBNO[1] = 101;
string sql = "INSERT INTO Distributors (distribno) VALUES (:DISTRIBNO)";
cnn = new Oracle.DataAccess.Client.OracleConnection(conString);
cnn.Open();
using (Oracle.DataAccess.Client.OracleCommand cmd = cnn.CreateCommand())
{
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.BindByName = true;
// To use ArrayBinding, we need to set ArrayBindCount
cmd.ArrayBindCount = numRecords;
cmd.CommandTimeout = 0;
cmd.Parameters.Add(
":DISTRIBNO",
Oracle.DataAccess.Client.OracleDbType.Int32,
BR_VOLMONTH,
ParameterDirection.Input);
cmd.ExecuteNonQuery();
}//using卡洛斯·梅里格。
https://stackoverflow.com/questions/7638249
复制相似问题