如何防止Sqlite数据库被锁定?

时间:2015-02-18 02:33:37

标签: c# sqlite

我在数据库上读了一个线程。当我点击menustrip时,它显示错误"数据库被锁定。"它有时只会发生。有什么方法可以防止数据库锁定?我试过WAL但它没有用。

读:

private void checkPort(string ipIN, char[] input, string output)
    {
        try
        {
            bool building = false;
            Thread beep = new Thread(Beep);
            using (SQLiteConnection c = new SQLiteConnection(dbconnection))
            {
                c.Open();
                string query = "select * from ALL_IO(nolock)";
                using (SQLiteCommand cmd = new SQLiteCommand(query, c))
                {
                    using (SQLiteDataReader dr = cmd.ExecuteReader())
                    {
                        int Contact;
                        while (dr.Read())
                        {
                            string _IP = dr.GetString(0);
                            string _IO_I = dr.GetString(1);
                            string _BuildingName = dr.GetString(4);
                            int IO_I = Convert.ToInt32(dr.GetString(1).Replace("DI ", ""));
                            if (dr.GetString(3) == "NC")
                            {
                                Contact = 1;
                            }
                            else
                            {
                                Contact = 0;
                            }
                            _tableName = dr.GetString(8);
                            string _name = dr.GetString(5);
                            var _active = dr.GetString(6);
                            var _status = dr.GetString(7);
                            if (_active == "Yes" && _status == "Enable")
                            {
                                 //Some condition check here
                            }

        }
        catch { }
    }

写作:

void contexMenuuu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        data = "";
        ToolStripItem item = e.ClickedItem;
        using (SQLiteConnection c = new SQLiteConnection(dbconnection))
        {
            c.Open();
            string sql = "select * from " + Properties.Settings.Default.TableName + "(nolock) where Name= '" + Properties.Settings.Default.LabelName.Replace(" DO", "") + "' ";
            using (SQLiteCommand cmd = new SQLiteCommand(sql, c))
            {
                using (SQLiteDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        _controllerIP = dr.GetString(0);
                        _IO = dr.GetString(1);
                        _IO_O = dr.GetString(2).Replace("DO ", "");
                        _Name = dr.GetString(4);
                        _Interval = Convert.ToInt32(dr.GetString(9));
                    }
                }
            }
        }
        if (item.Text == "Bypass Enable")
        {
            using (SQLiteConnection c = new SQLiteConnection(dbconnection))
            {
                //c.DefaultTimeout = 2000;
                c.Open();
                string sql = "update ALL_IO SET Active='Yes', Status='Bypass' where ControllerIP='" + _controllerIP + "' and DI='" + _IO + "';";
                using (SQLiteCommand cmd = new SQLiteCommand(sql, c))
                {
                    lock (lockobj)
                    {
                        //SQLiteConnection.ClearAllPools();
                        cmd.ExecuteNonQuery(); //Error occur here
                    }                     
                }

            }

        }

1 个答案:

答案 0 :(得分:0)

功能完成后,您必须关闭数据库连接以避免数据库锁定问题。为了执行查询,您打开了数据库连接,之后您没有关闭它。所以你需要关闭。