2013-05-16 63 views
1

我有以下錯誤:c#System.InvalidOperationException:此OracleTransaction已完成;它不再可用

System.InvalidOperationException: This OracleTransaction has completed; it is no longer usable 

試圖使插入到數據庫

這裏是代碼:

OracleTransaction myTrans = null; 
    OracleCommand cmd = new OracleCommand("INSERT INTO ZTMP_SAM_TB_ELAB_PDR " + 
    " VALUES (:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14)", connection); 
    myTrans = connection.BeginTransaction(); 
    cmd.Transaction = myTrans; 
    /* 
    (:facilityid,:taxid, 
    :plantcode,:capdr,:fullmetkeym2, 
    :corrmetidm2,:stopreadingm2, 
    :corrstopreadm2,:fullmetkeym1, 
    :corrmetidm1,:stopreadingm1,:corrstopreadm1, 
    :stopreadtimem2,:stopreadtimem1) 
    */ 

    file = new System.IO.StreamReader(path+FileUpload1.FileName); 
    while((line = file.ReadLine()) != null) 
    { 
     string[] split = line.Split(','); 


     cmd.Parameters.Add("1", OracleType.VarChar, 64).Value = split[0]; 
     cmd.Parameters.Add("2", OracleType.VarChar, 64).Value = ""; 
     cmd.Parameters.Add("3", OracleType.VarChar, 64).Value = ""; 
     cmd.Parameters.Add("4", OracleType.Number).Value = Convert.ToInt32(split[1]); 
     cmd.Parameters.Add("5", OracleType.VarChar, 64).Value = split[6]; 
     cmd.Parameters.Add("6", OracleType.VarChar, 64).Value = split[7]; 
     cmd.Parameters.Add("7", OracleType.Number).Value = Convert.ToInt32(split[8]); 
     cmd.Parameters.Add("8", OracleType.Number).Value = Convert.ToInt32(split[9]); 
     cmd.Parameters.Add("9", OracleType.VarChar, 80).Value = split[2]; 
     cmd.Parameters.Add("10", OracleType.VarChar, 80).Value = split[3]; 
     cmd.Parameters.Add("11", OracleType.Number).Value = Convert.ToInt32(split[4]); 
     cmd.Parameters.Add("12", OracleType.Number).Value = Convert.ToInt32(split[5]); 
     DateTime date1,date2; 

     DateTime.TryParseExact(split[10], "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out date1); 
     DateTime.TryParseExact(split[11], "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out date2); 

     cmd.Parameters.Add("13", OracleType.DateTime).Value = date1; 
     cmd.Parameters.Add("14", OracleType.DateTime).Value = date2; 
     try 
     { 
      cmd.ExecuteNonQuery(); 

      myTrans.Commit();  
     } 
     catch(Exception ex){ 

     myTrans.Rollback(); 
     Label1.Text = ex.Message; 
     //"Si e' verificato un errore nell'inserimento dei dati nella tabella ZTMP_SAM_TB_ELAB_PDR"; 
     } 
     /*Per ogni elemento esegui l'insert*/ 
    } 

回答

3

的問題是,您雖然循環提交事務幾次,假設您要提交所有插入的行

// ... 
file = new System.IO.StreamReader(path+FileUpload1.FileName); 
try 
{ 
    while((line = file.ReadLine()) != null) 
    { 
    // Parameters... 
    cmd.ExecuteNonQuery(); 
    } 

    myTrans.Commit();  
}     
catch(Exception ex) 
{ 
    myTrans.Rollback(); 
    // ... 
} 
+0

+1爲了簡潔退出。 –

+0

工作好,非常感謝你 –

0

試試這個:

bool flag=true; 
OracleTransaction myTrans = null; 
OracleCommand cmd = new OracleCommand("INSERT INTO ZTMP_SAM_TB_ELAB_PDR " + 
" VALUES (:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14)", connection); 
connection.open();//if not done already. 
myTrans = connection.BeginTransaction(); 
cmd.Transaction = myTrans; 

file = new System.IO.StreamReader(path+FileUpload1.FileName); 
while((line = file.ReadLine()) != null) 
{ 
    string[] split = line.Split(','); 


    cmd.Parameters.Add("1", OracleType.VarChar, 64).Value = split[0]; 
    cmd.Parameters.Add("2", OracleType.VarChar, 64).Value = ""; 
    cmd.Parameters.Add("3", OracleType.VarChar, 64).Value = ""; 
    cmd.Parameters.Add("4", OracleType.Number).Value = Convert.ToInt32(split[1]); 
    cmd.Parameters.Add("5", OracleType.VarChar, 64).Value = split[6]; 
    cmd.Parameters.Add("6", OracleType.VarChar, 64).Value = split[7]; 
    cmd.Parameters.Add("7", OracleType.Number).Value = Convert.ToInt32(split[8]); 
    cmd.Parameters.Add("8", OracleType.Number).Value = Convert.ToInt32(split[9]); 
    cmd.Parameters.Add("9", OracleType.VarChar, 80).Value = split[2]; 
    cmd.Parameters.Add("10", OracleType.VarChar, 80).Value = split[3]; 
    cmd.Parameters.Add("11", OracleType.Number).Value = Convert.ToInt32(split[4]); 
    cmd.Parameters.Add("12", OracleType.Number).Value = Convert.ToInt32(split[5]); 
    DateTime date1,date2; 

    DateTime.TryParseExact(split[10], "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out date1); 
    DateTime.TryParseExact(split[11], "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out date2); 

    cmd.Parameters.Add("13", OracleType.DateTime).Value = date1; 
    cmd.Parameters.Add("14", OracleType.DateTime).Value = date2; 
    try 
    { 
     cmd.ExecuteNonQuery();   
    } 
    catch(Exception ex) 
    { 
     myTrans.Rollback(); 
     Label1.Text = ex.Message; 
     flag=false; 
     break; 
    } 

} 
if(flag) 
    myTrans.Commit(); 
connection.close(); 

你所提交的內部,而循環交易,這就是爲什麼第一次迭代之後,交易犯兩次,這就是爲什麼你所得到的例外。

相反,在while循環完成後提交事務,如上面的代碼所示。

+0

如果發生異常,此代碼將執行回滾和提交 –

+0

對不起,這是一個錯誤,更新答案。 –

0

您爲每個循環調用一次提交。這將在第二個循環中失敗。您可以將提交圈外的或只是使用TransactionScope class

using(TransactionScope ts = new TransactionScope()) 
using(OracleConnection connection = new OracleConnection(cnstring)) 
{ 
    bool errorFound = false; 
    OracleCommand cmd = new OracleCommand("INSERT INTO ZTMP_SAM_TB_ELAB_PDR " + 
       " VALUES (:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14)", connection); 
    .... 
    file = new System.IO.StreamReader(path+FileUpload1.FileName); 
    while((line = file.ReadLine()) != null) 
    { 
     .... 
     try 
     { 
      cmd.ExecuteNonQuery(); 
     } 
     catch(Exception ex) 
     { 
      Label1.Text = ex.Message; 
      errorFound = true; 
      break; 
     } 
    } 
    if(!errorFound) ts.Complete(); 

} 

從使用塊whitout調用完成後會自動回滾您插入

+0

讓我試試吧 –

+0

我不能使用這個,因爲即時通訊使用.net 3.5 :(((( –

+0

爲什麼不能使用?它是從Framework 2.0開始可用的。有關Oracle的一些信息? – Steve

相關問題