您好我使用SQL批量插入到Oracle 10g Db。我使用ODP.NET因为我需要在表中插入20.000 - 40.000行。很长一段时间使用LINQ to SQL(对于oracle devart http://www.devart.com/linqconnect/)。 现在我必须使用“ADO.NET”对象,但我的代码看起来很糟糕。你能帮助我并提出我的想法如何折射吗?我想在代码的可读性和性能之间找到妥协。
抱歉我的英文
private void InitArrays(int size)
{
_id = new string[size];
_lm = new DateTime[size];
_priceWithoutDiscount = new decimal[size];
_priceWithDiscount = new decimal[size];
_talkTime = new int[size];
_type = new string[size];
_voiceNetwork = new string[size];
_callNo = new string[size];
_callDate = new DateTime[size];
_callType = new string[size];
_surname = new string[size];
_name = new string[size];
_no = new string[size];
}
private void PrepareArrays(IList<Call> calls)
{
InitArrays(calls.Count);
Parallel.For(0, calls.Count, i =>
{
_id[i] = IdGenerator.GenerateGuidForCall(calls[i]);
_no[i] = calls[i].Number;
_name[i] = calls[i].Name;
_surname[i] = calls[i].Surname;
_callType[i] = calls[i].CallType;
_callDate[i] = calls[i].Dt;
_callNo[i] = calls[i].CallingNumber;
_voiceNetwork[i] = calls[i].VoiceNetwork;
_type[i] = calls[i].Type;
_talkTime[i] = calls[i].TalkTimeInSec;
_priceWithDiscount[i] = (decimal)calls[i].PriceWithDiscount;
_priceWithoutDiscount[i] = (decimal)calls[i].PriceWithoutDiscount;
_lm[i] = DateTime.Now;
});
}
public void InsertCalls(IList<Call> calls)
{
PrepareArrays(calls);
string sql = "insert into r_calls (ID, NO, NAME, SURNAME, CALL_TYPE, CALL_DATE, CALL_NO, VOICE_NETWORK,"
+"TYPE, TALK_TIME,PRICE_WITH_DISCOUNT, PRICE_WITHOUT_DISCOUNT, LM_MODIFIED) "
+ "values (:id, :no, :name, :surname, :callType, :callDate, :callNo, :voiceNetwork, :type,"
+" :talkTime, :priceWithDiscount, :priceWithoutDiscount, :lm)";
var cnn = new OracleConnection(GenerateConnectionString());
cnn.Open();
OracleCommand cmd = cnn.CreateCommand();
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.BindByName = true;
// To use ArrayBinding, we need to set ArrayBindCount
cmd.ArrayBindCount = _id.Count();
// Instead of single values pass arrays of values as parameters
cmd.Parameters.Add(":id", OracleDbType.Varchar2,
_id, ParameterDirection.Input);
cmd.Parameters.Add(":no", OracleDbType.Varchar2,
_no, ParameterDirection.Input);
cmd.Parameters.Add(":name", OracleDbType.Varchar2,
_name, ParameterDirection.Input);
cmd.Parameters.Add(":surname", OracleDbType.Varchar2,
_surname, ParameterDirection.Input);
cmd.Parameters.Add(":callType", OracleDbType.Varchar2,
_callType, ParameterDirection.Input);
cmd.Parameters.Add(":callDate", OracleDbType.Date,
_callDate, ParameterDirection.Input);
cmd.Parameters.Add(":callNo", OracleDbType.Varchar2,
_callNo, ParameterDirection.Input);
cmd.Parameters.Add(":voiceNetwork", OracleDbType.Varchar2,
_voiceNetwork, ParameterDirection.Input);
cmd.Parameters.Add(":type", OracleDbType.Varchar2,
_type, ParameterDirection.Input);
cmd.Parameters.Add(":talkTime", OracleDbType.Decimal,
_talkTime, ParameterDirection.Input);
cmd.Parameters.Add(":priceWithDiscount", OracleDbType.Decimal,
_priceWithDiscount, ParameterDirection.Input);
cmd.Parameters.Add(":priceWithoutDiscount", OracleDbType.Decimal,
_priceWithoutDiscount, ParameterDirection.Input);
cmd.Parameters.Add(":lm", OracleDbType.Date,
_lm, ParameterDirection.Input);
cmd.ExecuteNonQuery();
cnn.Close();
}
答案 0 :(得分:0)
这对我来说很好看,但我建议在它上面运行StyleCop。
另外,您可以为SQL语句使用另一种格式:
string sqlStatement = @"
insert into TABLE
( COL1, COL2, COL3) VALUES
(:COL1, :COL2, :COL3)
";
还使用using
作为连接和命令。它实现了IDisposable
,因此可以包含在using
块中。
using(var connection = this.GetSomeConnection())
using(var command = this.InitTheCommandSomehow(connection))
{
// here your code can safely throw exceptions etc.
// and the connection will still be closed
...
cmd.Parameters.Add(
"id",
OracleDbType.Varchar2,
_id,
ParameterDirection.Input);
...
}
答案 1 :(得分:0)
这可能不是整个解决方案,但首先我会尝试避免参数名称的硬编码。我将从表模式本身获取param名称,然后通过它提交数据。这有点重构。我写了一篇关于此的文章,请查看:http://www.codeproject.com/KB/TipsnTricks/StoredProcSchemaSaving.aspx