1

我在下面有这段代码:

 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 = (long)ClonedSimulationDbCommand.ExecuteScalar(); 我收到一个数据库锁定异常

连接已打开,数据库路径存在且有效。我认为我打开同一数据库的连接这一事实可能是问题所在。

SQL 语句为:

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

谢谢!

4

1 回答 1

1

这可能是因为您正在尝试在该连接上执行另一个命令,而该连接仍在通过 DataReader 读取。

当数据读取器仍然有更多可用数据时,您不能使用连接来执行更多命令(除非数据库支持多个活动结果集——MARS),而 Sqlite 可能不支持。这意味着您必须在执行另一个命令之前读取整个第一个结果,或者使用不同的连接来执行此操作。

于 2015-11-23T18:39:24.370 回答