首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正在从数据库更新信息-错误数据库已锁定

正在从数据库更新信息-错误数据库已锁定
EN

Stack Overflow用户
提问于 2016-04-29 05:10:34
回答 1查看 62关注 0票数 0

我创建了一个类似测验的应用程序..当我尝试在标签中显示分数时,我得到了这个错误:

代码语言:javascript
复制
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进行注释。我该怎么解决它呢?

菜单代码:

代码语言:javascript
复制
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();
    }      
}

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2016-04-29 14:51:58

我找到了答案:我没有处置读者。现在它起作用了。

代码语言:javascript
复制
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;
    }    
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36925529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档