Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQLite数据库在更新后被锁定?_C#_Sqlite - Fatal编程技术网

C# SQLite数据库在更新后被锁定?

C# SQLite数据库在更新后被锁定?,c#,sqlite,C#,Sqlite,我有以下代码,用于进行更新查询: public void update(ClientResponse client) { db.ExecuteScalar("UPDATE PacientsOrder SET status = '"+client.status+"' WHERE pacient_id = '" + client.pacient_id + "' AND kind_work = '" + client.kind_work + "'"); // 2

我有以下代码,用于进行更新查询:

public void update(ClientResponse client)
        {
            db.ExecuteScalar("UPDATE PacientsOrder SET status = '"+client.status+"' WHERE pacient_id = '" + client.pacient_id + "' AND kind_work = '" + client.kind_work + "'"); // 222 Line
            db.ExecuteScalar("UPDATE Transactions SET http_code = '" + client.http_code+ "' WHERE pacient_id = '" + client.pacient_id+"'");
        }
其中
ExecuteScalar
是Db连接器的方法:

public string ExecuteScalar(string sql)
        {
            SQLiteConnection cnn = new SQLiteConnection(dbConnection); // 119 line
            cnn.Open();
            SQLiteCommand mycommand = new SQLiteCommand(cnn);
            mycommand.CommandText = sql;
            object value = mycommand.ExecuteScalar();
            cnn.Close();
            if (value != null)
            {
                return value.ToString();
            }
            return "";
        }
当我尝试执行上述查询时,我得到一个错误:

System.Data.SQLite.SQLiteException (0x80004005): database is locked
database is locked
   в System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
   в System.Data.SQLite.SQLiteDataReader.NextResult()
   в System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
   в System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
   в System.Data.SQLite.SQLiteCommand.ExecuteScalar(CommandBehavior behavior)
   в System.Data.SQLite.SQLiteCommand.ExecuteScalar()
   в SQLiteDatabase.ExecuteScalar(String sql) в D:\Projects\Library\SQLiteDatabase.cs:строка 119
   Pacients.update(ClientResponse client) в D:\Projects\c-tests-u Controllers\Pacients\Pacients.cs:строка 222
我不明白这个错误的原因

这是我的删除方法:

public bool Delete(String tableName, String where)
        {
            Boolean returnCode = true;
            try
            {
                this.ExecuteNonQuery(String.Format("delete from {0} where {1};", tableName, where));
            }
            catch (Exception fail)
            {
                MessageBox.Show(fail.Message);
                returnCode = false;
            }
            return returnCode;
        }
public bool Delete(String tableName, String where)
        {

            string sql = String.Format("delete from {0} where {1};", tableName, where);

            using (SQLiteConnection cnn = new SQLiteConnection(dbConnection))
            using (SQLiteCommand mycommand = new SQLiteCommand(sql, cnn))
            {
                cnn.Open();
                object value = mycommand.ExecuteNonQuery();
                return (value != null) ? value.ToString() : "";
            }
        }
更新的SQL删除方法:

public bool Delete(String tableName, String where)
        {
            Boolean returnCode = true;
            try
            {
                this.ExecuteNonQuery(String.Format("delete from {0} where {1};", tableName, where));
            }
            catch (Exception fail)
            {
                MessageBox.Show(fail.Message);
                returnCode = false;
            }
            return returnCode;
        }
public bool Delete(String tableName, String where)
        {

            string sql = String.Format("delete from {0} where {1};", tableName, where);

            using (SQLiteConnection cnn = new SQLiteConnection(dbConnection))
            using (SQLiteCommand mycommand = new SQLiteCommand(sql, cnn))
            {
                cnn.Open();
                object value = mycommand.ExecuteNonQuery();
                return (value != null) ? value.ToString() : "";
            }
        }

像SQLiteCommand这样的一次性对象,特别是SQLiteConnection,应该尽快处理。为了促进这种模式,应该在这些对象周围使用

public string ExecuteScalar(string sql)
{
    using(SQLiteConnection cnn = new SQLiteConnection(dbConnection))
    using(SQLiteCommand mycommand = new SQLiteCommand(sql, cnn))
    {
        cnn.Open();
        object value = mycommand.ExecuteScalar();
        return (value != null ? value.ToString() : "";
    }
}

我建议您将此方法命名为ExecuteOnQuery,而不是ExecuteScalar,并调用SQLiteCommand ExecuteOnQuery。ExecuteScalar具有不同的含义,将其用于更新操作令人困惑

不处理
mycommand
可能是原因……正如上次您询问我的代码时所解释的,与之对应的区别是什么?您是否建议替换整个方法?当连接
cnn
关闭时?当代码从使用块的关闭括号中退出时,它将自动关闭。Using语句是替换try的一种优雅方式……最后,finally块包含在Using起始行中创建的对象的dispose。对于DbConnection,dispose方法调用Close方法。因此,如果出现异常,您的连接也将始终关闭。using块出口处的dispose调用将关闭,然后释放保留在数据库文件上的所有锁。在此之后,您不应该有任何挂起的锁。当然,我看不到您的所有代码,但是如果定期使用此模式,可以保证正确处理数据库文件。为什么建议仅在更新时使用此方法?