2011-08-24 48 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部署我相信 –