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

时间:2017-11-04 21:37:43

标签: 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 + "'"); // 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;
        }

更新了SQL删除方法:

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() : "";
            }
        }

1 个答案:

答案 0 :(得分:2)

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

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() : "";
    }
}

我要建议您将此方法命名为ExecuteNonQuery而不是ExecuteScalar,并调用SQLiteCommand ExecuteNonQuery。 ExecuteScalar具有不同的含义,并将其用于UPDATE操作会让人感到困惑