C# 数据库 SQLite 锁定数据库错误

标签 c# sqlite

我目前正在使用 C# 和 SQLite 编写代码。抛出一个错误,指出数据库在消息框中被锁定了两次。

该查询在 SQLite DB 浏览器上工作,但是,当它被放置在 C# 代码中时,它会引发错误。

这是给我一个错误的代码:

cmd.CommandText = "UPDATE customers SET Bill = Bill - "+textBox2.Text+" WHERE customers.CustomerID = " + textBox1.Text + ";";

等号似乎有问题,可能是算术过程有问题。

完整代码:
 SQLiteConnection myconn = new SQLiteConnection(@"Data Source = C:\Users\chick\Newspaper.db");
        SQLiteCommand cmd = myconn.CreateCommand();
        cmd.CommandText = "UPDATE customers SET Bill = (Bill - "+textBox2.Text+") WHERE customers.CustomerID = " + textBox1.Text + ";";
        myconn.Open();
        try
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("Succesfully Update");
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

更新:
将格式更改为 using() {}但是它仍然无法正常工作。程序崩溃

新代码:
   using (SQLiteConnection myconn = new SQLiteConnection(@"Data Source = C:\Users\chick\Newspaper.db"))
        {

            var sql = "Update customers SET Bill = Bill - @pay WHERE customers.CustomerID = @cid;";
            myconn.Open();
            using (var cmd = new SQLiteCommand(sql, myconn))
            {
                cmd.Parameters.AddWithValue("@cid", textBox1.Text);
                cmd.Parameters.AddWithValue("@pay", textBox2.Text);
                cmd.ExecuteNonQuery();
            }
        }

最佳答案

问题是程序的其他部分(或其他程序)仍然具有事件事务。

您必须正确清理所有访问数据库的命令,即使用 using到处。

关于C# 数据库 SQLite 锁定数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50259679/

相关文章:

c# - 缓存过期,尽管明确设置为不过期

c# - 选择对象列表中对象的属性,该对象也在另一个对象列表中

sqlite - 使用 CLion 和 CMAKE 的 sqlite3 函数的 C/C++ 未定义引用

java - 是否可以在 Web 应用程序中使用 SQLite 数据库

sql-server - SQL Server 2008 Express 到 SQLite

c# - 使用 double 组时内存泄漏

c# - 在 java 中调用 .net 程序集的最佳且简单的方法

c# - 使用 MVC/MCP 时如何在没有 switch 语句的情况下处理 UI 的派生类?

android - SQLite 向表中添加一个列表

android - Sqlite 列 id 不唯一