2011-08-24 243 views
0

我正在尝试从C#向Oracle进行批量插入。我有对象的数组列表中的数据。目前插入为:如何使用C#进行Oracle批量插入?

using (OracleCommand command = new OracleCommand(commandString, oc.connection))//, _transaction)) 
    { 
    string[] temp = netstat.getStrings(); 

    //replace with nulls 

    command.Parameters.Add("node", OracleType.VarChar, 255).Value = temp[0]; 
    command.Parameters.Add("protocol", OracleType.VarChar, 10).Value = temp[1]; 
    command.Parameters.Add("localip", OracleType.VarChar, 25).Value = temp[2]; 
    command.Parameters.Add("localport", OracleType.VarChar, 10).Value = temp[3]; 
    command.Parameters.Add("foreignip", OracleType.VarChar, 25).Value = temp[4]; 
    command.Parameters.Add("foreignport", OracleType.VarChar, 10).Value = temp[5]; 

    if (temp[6] == null) 
     { 
     command.Parameters.Add("state", OracleType.VarChar, 25).Value = DBNull.Value; 
     } 
    else 
     { 
     command.Parameters.Add("state", OracleType.VarChar, 25).Value = temp[6]; 
     } 

    command.Parameters.Add("pid", OracleType.VarChar, 10).Value = temp[7]; 
    try 
     { 
     command.ExecuteNonQuery(); 
     } 
    catch (OracleException e) 
     { 
     string errorMessage = "Code: " + e.Code + "\n" + 
           "Message: " + e.Message; 

     //System.Diagnostics.EventLog log = new System.Diagnostics.EventLog(); 
     //log.Source = "My Application"; 
     //log.WriteEntry(errorMessage); 
     Console.WriteLine("An exception occurred. Please contact your system administrator. " + errorMessage); 
     } 
    } 

这需要大约10秒每个对象!有没有更快的方法来做到这一点?我正在使用Microsoft System.Data.OracleClient。我应该考虑切换吗?我的目标是速度和应用程序可移植性......我目前正在将该exe文件与oracle调用接口DLL一起分发,因此它将在未安装Oracle的情况下运行。

回答

1

MS客户端不是很好。你最好用ODP.net

+0

ODP.net能让我保持相同的便携性吗?或者需要额外的文件/安装oracle来运行这个程序? – coergo

+0

我上次使用它需要安装一个oracle客户端。现在你可以做一个xcopy部署我相信 –