使用c#应用程序的Sqlite数据库锁定错误

时间:2012-09-26 05:40:33

标签: c#

我是我的c#应用程序,我使用sqlite作为后端。当我执行简单的插入查询时,它花了我太长时间,结束了数据库锁定消息。我在舞台服务器中部署了我的应用程序并远程执行操作。我的代码请帮助。

String sqlExpr = "INSERT INTO item (id, typeid, ownerid, created, modifiedby, modified,active,imageuploaded,logouploaded,language_item) VALUES (@Id,@type_TypeId ,@db_currentUser,@dates,@db_id ,@modified,@active_status,@image,@logo,@language)";

                    //using (SQLiteCommand _insertItem = new SQLiteCommand())
                    //{
                    SQLiteCommand _insertItem = new SQLiteCommand();
                        _insertItem.CommandText = sqlExpr;
                        _insertItem.Parameters.AddWithValue("@Id", Id);
                        _insertItem.Parameters.AddWithValue("@type_TypeId", type.TypeId);
                        _insertItem.Parameters.AddWithValue("@db_currentUser", db.currentUser.id);
                        _insertItem.Parameters.AddWithValue("@dates", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        _insertItem.Parameters.AddWithValue("@db_id", db.currentUser.id);
                        _insertItem.Parameters.AddWithValue("@modified", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        _insertItem.Parameters.AddWithValue("@active_status", active_status);
                        _insertItem.Parameters.AddWithValue("@image", "false");
                        _insertItem.Parameters.AddWithValue("@logo", "false");
                        _insertItem.Parameters.AddWithValue("@language", language);
                        rowsAffected = db.ExecuteNonQuerySQL(_insertItem);
                        db.Close();


 public int ExecuteNonQuerySQL(SQLiteCommand cmd)
        {
            int ireturn = 0;

            if (conn.State != ConnectionState.Open)//the connection is individual for each session so no need of lock .added the code on 2011-11-08 by Sangeetha
                Open(DataFile);
            //SQLiteConnection C = new SQLiteConnection("Data Source=database.db;Version=3;New=False;Compress=True;");

            using (SQLiteTransaction dbtrans = conn.BeginTransaction(IsolationLevel.ReadCommitted))
            {
                using (SQLiteCommand cmd1 = (SQLiteCommand)cmd.Clone())
                {

                    cmd.Dispose();
                    cmd1.Connection = conn;
                    cmd1.Transaction = dbtrans;
                    ireturn = cmd1.ExecuteNonQuery();
                    dbtrans.Commit();
                    cmd1.Dispose();
                }
            }


            return ireturn;
        }

谢谢

1 个答案:

答案 0 :(得分:0)

也许在您打开连接之前它仍然打开。确保在退出函数之前处理所有连接和命令(使用USING语句)并关闭每个阅读器。

或迁移到' oledb'提供者(MS ACCESS)