sqlite数据库锁定错误

时间:2012-03-09 10:27:36

标签: c# sqlite

在我的c#应用程序中我正在使用sqlite数据库。我的c#应用程序在另一台机器上作为服务运行。我从本地机器执行CRUD操作,对我们开发的Flex应用程序的数据库执行CRUD操作。 现在问题是当我尝试从前端删除或添加项目到数据库时,有时我在数据库被锁定时收到错误。请在下面找到删除数据的代码片段。

连接字符串

public ThingzDatabase(string dataFile)
        {
            _dataFile = dataFile;
            conn = new SQLiteConnection("data source=" + dataFile+";Version=3;");
        }

用于执行以下方法的命令。

 public SQLiteDataReader ExecuteSQL(String sqlExpr)
        {

            if (conn.State != ConnectionState.Open)
                Open(DataFile);

                SQLiteCommand cmd = conn.CreateCommand();
                cmd.CommandText = sqlExpr;
                cmd.CommandType = CommandType.Text;
                return cmd.ExecuteReader();

        }

        public int ExecuteNonQuerySQL(String sqlExpr)
        {
            if (conn.State != ConnectionState.Open)
                Open(DataFile);

            int ireturn = 0;

            using (SQLiteTransaction dbtrans = conn.BeginTransaction())
            {
                SQLiteCommand cmd = conn.CreateCommand();
                cmd.CommandText = sqlExpr;

                ireturn = cmd.ExecuteNonQuery();
                dbtrans.Commit();
            }

            return ireturn;
        }

现在,下面是用于删除关系的代码,

OrderedRelationSet orderedRelationSet = new OrderedRelationSet(SessionDatabase, relation.RelationTypeId, relation.SourceThingId);

public OrderedRelationSet(ThingzDatabase db, int relationTypeId, int parentItemId)
        {
            this.db = db;
            this.relationTypeId = relationTypeId;
            this.parentItemId = parentItemId;
            childId = ThingzDatabase.thingsId;
            relations = new List<Relation>();


            SQLiteDataReader relationsReader = db.ExecuteSQL(
               @"SELECT *
                  FROM   relation
                  WHERE  parent_itemid = " + this.parentItemId.ToString() + @" and child_itemid=" + childId + @" and
                         relation_typeid = " + this.relationTypeId.ToString()
           );
            while (relationsReader.Read())
            {
                Relation relation = new Relation();

                relation.db = db;
                relation.SourceThingId = Convert.ToInt32(relationsReader["parent_itemid"]);
                relation.ThingId = Convert.ToInt32(relationsReader["child_itemid"]);
                relation.Id = Convert.ToInt32(relationsReader["id"]);
                relation.RelationTypeId = Convert.ToInt32(relationsReader["relation_typeid"]);

                relation.ordinal = Utilities.OptionalIntFieldFromSQLReader("ordinal", relationsReader);

                relation.thingIsChild = true;

                relations.Add(relation);
            }
            relationsReader.Close();
            reorder();
        }

----------

case "remove":
                                try
                                {
                                    if (orderedRelationSet.RemoveRelation(relation) != true)
                                    {
                                        TouchServer.Log(Logger.MessageType.Error, 0, "Remove relation operation failed");
                                        throw new Exception("remove relation failed");
                                    }
                                }
                                catch (Exception)
                                {
                                    if (orderedRelationSet.RemoveRelation(relation) != true)
                                    {
                                        TouchServer.Log(Logger.MessageType.Error, 0, "Remove relation operation failed");
                                        throw new Exception("remove relation failed");

这是我的代码....请让我知道修复此锁定问题的解决方案是什么。 Thakx提前

}                                 }

1 个答案:

答案 0 :(得分:1)

我知道SQLite一次只能处理一次写入,但可以处理多个同时读取

  

有时我在数据库锁定时遇到错误

同时写入同时写入

您可以查看此question