2017-08-18 64 views
1

我當前正在研究一個應用程序,我需要在Oracle(11g)基礎(使用ODP.NET)中同時插入數千行。 (「批量插入樣式」)數組綁定與Oracle(11g)Db和c#

這就是爲什麼我試圖使用ODP.net的Oracle數據庫的數組綁定技術。

這裏是我的代碼:運行時沒有錯誤,但沒有任何反應,它永遠運行。

如果有人知道問題可能來自哪裏,請告訴我。

感謝您的幫助。

private static void AddDataTableToDataBase(DataTable tableLog) 
    { 
     string[] type = new string[tableLog.Rows.Count]; 
     DateTime[] timestamp = new DateTime[tableLog.Rows.Count]; 
     string[] source = new string[tableLog.Rows.Count]; 
     string[] appName = new string[tableLog.Rows.Count]; 
     string[] action = new string[tableLog.Rows.Count]; 
     string[] fileType = new string[tableLog.Rows.Count]; 
     string[] usr = new string[tableLog.Rows.Count]; 
     int?[] executionTime = new int?[tableLog.Rows.Count]; 
     string[] addMetadata = new string[tableLog.Rows.Count]; 
     string[] explanation = new string[tableLog.Rows.Count]; 


     for (int i = 0; i < tableLog.Rows.Count; i++) 
     { 
      type[i] = tableLog.Rows[i][0].ToString(); 
      timestamp[i] = (DateTime)tableLog.Rows[i][1]; 
      source[i] = tableLog.Rows[i][2].ToString(); 
      appName[i] = tableLog.Rows[i][3].ToString(); 
      action[i] = tableLog.Rows[i][4].ToString(); 
      fileType[i] = tableLog.Rows[i][5].ToString(); 
      usr[i] = tableLog.Rows[i][6].ToString(); 
      int executionTimeValue; 
      if (int.TryParse(tableLog.Rows[i][7].ToString(), out executionTimeValue)) 
       executionTime[i] = executionTimeValue; 

      addMetadata[i] = tableLog.Rows[i][8].ToString(); 
      explanation[i] = tableLog.Rows[i][9].ToString(); 
     } 
     string OracleConnectionString = ConfigurationManager.ConnectionStrings["DmsConnection"].ConnectionString; 
     (OracleConnectionString); 
     Oracle.DataAccess.Client.OracleConnection conn = new Oracle.DataAccess.Client.OracleConnection(OracleConnectionString); 
     conn.Open(); 
        Oracle.DataAccess.Client.OracleCommand cmd = conn.CreateCommand(); 
     cmd.CommandText = "INSERT INTO LOG (TYPE,TIMESTAMP,SOURCE,APPNAME,ACTION,FILETYPE,USR,EXECUTIONTIME,ADDMETADATA,EXPLANATION) VALUES (:TYPE, :TIMESTAMP, :SOURCE, :APPNAME, :ACTION, :FILETYPE, :USR, :EXECUTIONTIME, :ADDMETADATA, :EXPLANATION)"; 

     Oracle.DataAccess.Client.OracleParameter TYPE = new Oracle.DataAccess.Client.OracleParameter("TYPE", Oracle.DataAccess.Client.OracleDbType.NVarchar2, 150, "TYPE"); 
     Oracle.DataAccess.Client.OracleParameter TIMESTAMP = new Oracle.DataAccess.Client.OracleParameter("TIMESTAMP", Oracle.DataAccess.Client.OracleDbType.Date); 
     Oracle.DataAccess.Client.OracleParameter SOURCE = new Oracle.DataAccess.Client.OracleParameter("SOURCE", Oracle.DataAccess.Client.OracleDbType.NVarchar2, 150); 
     Oracle.DataAccess.Client.OracleParameter APPNAME = new Oracle.DataAccess.Client.OracleParameter("APPNAME", Oracle.DataAccess.Client.OracleDbType.NVarchar2, 150); 
     Oracle.DataAccess.Client.OracleParameter ACTION = new Oracle.DataAccess.Client.OracleParameter("ACTION", Oracle.DataAccess.Client.OracleDbType.NVarchar2, 150); 
     Oracle.DataAccess.Client.OracleParameter FILETYPE = new Oracle.DataAccess.Client.OracleParameter("FILETYPE", Oracle.DataAccess.Client.OracleDbType.NVarchar2, 150); 
     Oracle.DataAccess.Client.OracleParameter USR = new Oracle.DataAccess.Client.OracleParameter("USR", Oracle.DataAccess.Client.OracleDbType.NVarchar2, 150); 
     Oracle.DataAccess.Client.OracleParameter EXECUTIONTIME = new Oracle.DataAccess.Client.OracleParameter("EXECUTIONTIME", Oracle.DataAccess.Client.OracleDbType.Int32); 
     Oracle.DataAccess.Client.OracleParameter ADDMETADATA = new Oracle.DataAccess.Client.OracleParameter("ADDMETADATA", Oracle.DataAccess.Client.OracleDbType.Clob); 
     Oracle.DataAccess.Client.OracleParameter EXPLANATION = new Oracle.DataAccess.Client.OracleParameter("EXPLANATION", Oracle.DataAccess.Client.OracleDbType.Clob); 

     TYPE.Direction = ParameterDirection.Input; 
     TIMESTAMP.Direction = ParameterDirection.Input; 
     SOURCE.Direction = ParameterDirection.Input; 
     APPNAME.Direction = ParameterDirection.Input; 
     ACTION.Direction = ParameterDirection.Input; 
     FILETYPE.Direction = ParameterDirection.Input; 
     USR.Direction = ParameterDirection.Input; 
     EXECUTIONTIME.Direction = ParameterDirection.Input; 
     ADDMETADATA.Direction = ParameterDirection.Input; 
     EXPLANATION.Direction = ParameterDirection.Input; 

     cmd.Parameters.Add(TYPE); 
     cmd.Parameters.Add(TIMESTAMP); 
     cmd.Parameters.Add(SOURCE); 
     cmd.Parameters.Add(APPNAME); 
     cmd.Parameters.Add(ACTION); 
     cmd.Parameters.Add(FILETYPE); 
     cmd.Parameters.Add(USR); 
     cmd.Parameters.Add(EXECUTIONTIME); 
     cmd.Parameters.Add(ADDMETADATA); 
     cmd.Parameters.Add(EXPLANATION); 

     cmd.Parameters["TYPE"].Value = type; 
     cmd.Parameters["TIMESTAMP"].Value = timestamp; 
     cmd.Parameters["SOURCE"].Value = source; 
     cmd.Parameters["APPNAME"].Value = appName; 
     cmd.Parameters["ACTION"].Value = action; 
     cmd.Parameters["FILETYPE"].Value = fileType; 
     cmd.Parameters["USR"].Value = usr; 
     cmd.Parameters["EXECUTIONTIME"].Value = executionTime; 
     cmd.Parameters["ADDMETADATA"].Value = addMetadata; 
     cmd.Parameters["EXPLANATION"].Value = explanation; 

     cmd.ExecuteNonQuery(); 
     cmd.Dispose(); 
     conn.Close(); 
    } 
+0

你能夠逐句通過代碼來確定它卡在哪一行嗎?如果您無法逐句通過代碼,那麼您可以添加一些打印語句嗎? – davmos

+0

嗨達沃斯感謝您的評論。 它卡在「executeNonQuey()」指令上。 我已經嘗試了許多不同的查詢值,這是唯一不會引發錯誤的值。 – LaGranf

+0

好吧,你試圖插入多少行?也許這只是需要很長時間。你可以試一下嗎?如果它仍然沒有返回,也許表被鎖定,並且正在等待該鎖被釋放。如果你已經執行了先前的插入或更新並且沒有被提交,它可能被另一個用戶/進程或甚至你自己鎖定! – davmos

回答

0

我終於找到了解決我的問題。

上面的代碼工作正常,唯一的問題是我使用Clob Oracle數據類型。從字符串到Clob的內部轉換需要很長時間。

我結束了截斷太長的字符串,現在它工作正常,它非常快。 (只需幾秒鐘即可插入4000條記錄)。