我目前正在使用 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/