我创建了一个类似测验的应用程序..当我尝试在标签中显示分数时,我得到了这个错误:
An unhandled exception of type 'System.Data.SQLite.SQLiteException' occurred in System.Data.SQLite.dll Additional information: database is locked database is locked我知道哪一个是问题所在,但我不知道如何解决它。这个问题肯定存在于菜单表单(“最高父级”)。我知道这是因为应用程序从测验表单更新数据库,但当它返回并读取数据库以便我可以更改标签时,我收到了这个错误。
update_score() -reads数据库和更改标签
结论:我不能在标签中显示最高分两次:当我打开应用程序时,当我从测验form..So返回时,我必须在显示对话框之后(用户返回时不会知道分数)或在Meniu /Meniu_Load (它在开始时不显示)中对update_score进行注释。我该怎么解决它呢?
菜单代码:
public partial class Meniu : Form
{
SQLiteConnection con;
public bool con_opened = false; // tin minte daca am deschis conexiunea
public int bs_lit=0, bs_info = 0;
public Meniu()
{
//this.StartPosition = FormStartPosition.CenterScreen;
InitializeComponent();
con_opened=false;
update_score();
}
private void Meniu_Load(object sender, EventArgs e)
{
//con_opened=false;
//update_score();
}
public void db1_read()
{
if (con_opened == false)
{
con = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");//Version=3;
con_opened = true;
con.Open();
}
string sql = "select * from bestscore WHERE materie LIKE 'literatura'";
SQLiteCommand command = new SQLiteCommand(sql, con);
SQLiteDataReader reader = command.ExecuteReader();
reader.Read();
bs_lit = Int32.Parse(reader[1].ToString());
command.Dispose();
sql = "select * from bestscore WHERE materie LIKE 'informatica'";
SQLiteCommand cmd = new SQLiteCommand(sql, con);
SQLiteDataReader rdr = cmd.ExecuteReader();
rdr.Read();
bs_info = Int32.Parse(rdr[1].ToString());
cmd.Dispose();
con.Close();
con_opened = false;
}
private void update_score()
{
db1_read();
lbl_bs_info.Text = bs_info.ToString();
lbl_bs_lit.Text = bs_lit.ToString();
}
private void btn_literatura_testare_Click(object sender, EventArgs e)
{
testare flit = new testare();
this.Hide();
flit.reveal = false;
flit.materie = "literatura";
flit.ShowDialog();
update_score(); // if the function is here and in Meniu()
// or Meniu_load()I receive the error
// if it`s just one of them
//it works just fine
if (flit.reveal == true)
this.Show();
else
Application.Exit();
}
}谢谢!
发布于 2016-04-29 14:51:58
我找到了答案:我没有处置读者。现在它起作用了。
public void db1_read()
{
if (con_opened == false)
{
con = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");//Version=3;
con_opened = true;
con.Open();
}
string sql = "select * from bestscore WHERE materie LIKE 'literatura'";
SQLiteCommand command = new SQLiteCommand(sql, con);
SQLiteDataReader reader = command.ExecuteReader();
reader.Read();
bs_lit = Int32.Parse(reader[1].ToString());
command.Dispose();
reader.Dispose();
sql = "select * from bestscore WHERE materie LIKE 'informatica'";
SQLiteCommand cmd = new SQLiteCommand(sql, con);
SQLiteDataReader rdr = cmd.ExecuteReader();
rdr.Read();
bs_info = Int32.Parse(rdr[1].ToString());
cmd.Dispose();
rdr.Dispose();
con.Close();
con_opened = false;
} https://stackoverflow.com/questions/36925529
复制相似问题