2015-11-23 46 views
1

我具有低於此代碼的SQLite的DLL:數據庫被鎖定爲C#

public Dictionary<long, List<long>> CloneGroupMeasuredOutputs(ISimulation clonedSimulation, Dictionary<long, long> crashDict, Dictionary<long, long> outputDict, string variationIDs, ProgressInterface progressInterface) 
    { 
     { 
      Dictionary<long, List<long>> measuredOutputIndexes = new Dictionary<long, List<long>>(); 
      DbDataReader reader = null; 
      long counter = 0; 
      string crashText = GetKeysList(crashDict); 
      string outputText = GetKeysList(outputDict); 
      int numberOfIterations = 50000; 
      // 
      var conn = ((RDB1Connection)this.DbConnection).RDB1DbConnection; 

      try 
      { 
       if (conn.State == ConnectionState.Closed) conn.Open(); 

       DbCommand dbCommand = conn.CreateCommand(); 
       dbCommand.Connection = conn; 

       dbCommand.CommandText = string.Format("CREATE INDEX IF NOT EXISTS MOIndexSCOVI ON MeasuredOutputs (SimulationId, CrashId, OutputId, VariationIndex)"); 
       dbCommand.ExecuteNonQuery(); 

       dbCommand.CommandText = string.Format(_sqlGetNumberOfSomeMeasuredOutputs, this.ID, crashText, outputText, variationIDs); 
       if (conn.State == ConnectionState.Closed) conn.Open(); 

       long numberOfRows = (long)dbCommand.ExecuteScalar(); 
       float step = (float)40.0/(numberOfRows/numberOfIterations + 1); 

       dbCommand.CommandText = string.Format(_sqlGetSomeMeasuredOutputs, this.ID, crashText, outputText, variationIDs); 

       if (conn.State == ConnectionState.Closed) conn.Open(); 
       reader = dbCommand.ExecuteReader(); 

       DbCommand ClonedSimulationDbCommand = ((RDB1Connection)clonedSimulation.DbConnection).DbProviderFactory.CreateCommand(); 
       ClonedSimulationDbCommand.Connection = ((RDB1Connection)clonedSimulation.DbConnection).RDB1DbConnection; 


       if (clonedSimulation.IsConnectionClosed()) 
        clonedSimulation.OpenConnection(); 

       long _id; 
       List<long> measuredOutputsIds = null; 

       while (reader.Read()) 
       { 
        var p0 = (double)reader["value"]; 
        var p1 = (long)reader["runIndex"]; 
        var p2 = crashDict[(long)reader["crashId"]]; 
        var p3 = outputDict[(long)reader["outputId"]]; 
        var p4 = (long)reader["variationIndex"]; 


        ClonedSimulationDbCommand.CommandText = string.Format(_sqlInsertRowIntoMeasuredOutputs, p0, p1, p2, p3, p4, (long)clonedSimulation.ID); 

        measuredOutputsIds = new List<long>(); 
        // p2.Value = crashId; 
        _id = (long)ClonedSimulationDbCommand.ExecuteScalar(); 
        //_id = (long)dbCommand.ExecuteScalar(); 
        measuredOutputsIds.Add(_id); 
        counter++; 
        measuredOutputIndexes.Add((long)reader["id"], measuredOutputsIds); 

        if (counter >= numberOfIterations) 
        { 
         counter = 0; 
         ((RDB1Connection)clonedSimulation.DbConnection).CommitTransaction(); 
         if (progressInterface.isCancelled()) 
         { 
          reader.Close(); 
          return null; 
         } 
         else 
         { 
          progressInterface.updateProgressWith(step); 
         } 
         //((RDB1Connection)clonedSimulation.DbConnection).BeginTransaction(); 
        } 
       } 
       reader.Close(); 
       ((RDB1Connection)clonedSimulation.DbConnection).CommitTransaction(); 

       return measuredOutputIndexes; 
      } 
      catch (Exception ex) 
      { 
       ((RDB1Connection)clonedSimulation.DbConnection).RollbackTransaction(); 
       throw ex; 
      } 
      finally 
      { 
       if (reader != null) 
        reader.Close(); 
      } 
     } 
    } 

在該行:_id =(長)ClonedSimulationDbCommand.ExecuteScalar();我收到數據庫鎖定異常

連接處於打開狀態,數據庫的路徑存在並且有效。我認爲,我打開連接到同一個數據庫的事實可能是問題所在。

的SQL語句是:

_sqlInsertRowIntoMeasuredOutputs = @"INSERT INTO MeasuredOutputs (Value,RunIndex,CrashId,OutputId,VariationIndex,SimulationId) VALUES({0},{1},{2},{3},{4},{5}); SELECT last_insert_rowid()"; 

謝謝!

回答

1

很可能是因爲當連接仍在通過DataReader讀取時,您正試圖在連接上執行另一個命令。

不能使用用於執行多個命令,而DataReader的仍然有提供給它更多的數據連接(除非該數據庫支持多活動結果集 - MARS),這SQLite的可能沒有。這意味着您必須在執行另一個命令之前讀取整個第一個結果,或使用其他連接來完成。

+0

我發現,你在閱讀時不能插入。所以你必須閱讀所有內容並在之後插入。 –