当前位置: 代码迷 >> C# >> c#操作SQLite出现database is locked,该怎么解决
  详细解决方案

c#操作SQLite出现database is locked,该怎么解决

热度:473   发布时间:2016-05-05 05:28:34.0
c#操作SQLite出现database is locked
        private void button3_Click(object sender, EventArgs e)
        {
            string tm="";          //条码
            string mc = "";        //名称
            int cm = 0;             //尺码
            string ys = "";         //颜色

 
            
            int sl = 0;             //数量
            int sj = 0;             //售价
            int cb = 0;             //成本
            int zdsj = 0;           //最低售价,折扣价
            tm = textBox3.Text; 
            mc = textBox4.Text;
            cm = Convert.ToInt32(textBox5.Text);
            ys = textBox6.Text;


            

            
            sl = Convert.ToInt32(textBox10.Text);
            sj = Convert.ToInt32(textBox11.Text);
            cb = Convert.ToInt32(textBox12.Text);

       // tm;mc;cm;ys;jj;lx;cl入数据库yf表,需要先判断是否有数据
       // tm;sl(dj)入数据库sj表
       // tm;sl入数据库kc表,同时计入rk表
       // tm;sj入数据库jg表
            conn.Open();
            SQLiteCommand cmdcheck = conn.CreateCommand();            
            cmdcheck.CommandText = "select tm from yf where tm=" + tm;
            SQLiteDataReader readercheck = cmdcheck.ExecuteReader();
            
            if (readercheck.HasRows)  //如果有条码,则update数据
            {

                SQLiteCommand cmdupdate = conn.CreateCommand();
                cmdupdate.CommandText = "update yf set mc='" + mc + "',cm='" + cm + "',ys='" + ys + "' where tm=" + tm
                    + ";" +
                    "update jg set dj=" + sj + ",cb=" + cb + ",zdsj=" + zdsj + " where tm=" + tm
                    + ";" +
                    "update kc set kcl=kcl+" + sl + " where tm=" + tm
                    + ";" +
                    "insert into rk(tm,rkl,rksj) values ('" + tm + "','" + sl + "','" + DateTime.Now.ToString("yyyyMMdd") + "')";
                int id=cmdupdate.ExecuteNonQuery();
                //cmdupdate.Dispose();




                ////更新衣服表
                //using(SQLiteCommand cmdupdate = conn.CreateCommand())
                //{
                //    cmdupdate.CommandText = "update yf set mc='" + mc + "',cm='" + cm + "',ys='" + ys + "' where tm=" + tm;                
                //    cmdupdate.ExecuteNonQuery();
                //}

                ////更新售价表
                //using (SQLiteCommand cmdsj = conn.CreateCommand())
                //{
                //    cmdsj.CommandText = "update jg set dj=" + sj + ",cb=" + cb + ",zdsj=" + zdsj + " where tm=" + tm;
                //    cmdsj.ExecuteNonQuery();
                //}

                ////更新库存表
                //using (SQLiteCommand cmdkc = conn.CreateCommand())
                //{
                //    cmdkc.CommandText = "update kc set kcl=kcl+" + sl + " where tm=" + tm;
                //    cmdkc.ExecuteNonQuery();
                //}

                ////插入入库表
                //using (SQLiteCommand cmdrk = conn.CreateCommand())
                //{
                //    cmdrk.CommandText = "insert into rk(tm,rkl,rksj) values ('" + tm + "','" + sl + "','" + DateTime.Now.ToString("yyyyMMdd") + "')";
                //    cmdrk.ExecuteNonQuery();
                //}
                
                MessageBox.Show("更新入库成功", "更新入库");
            }
            else  //如果没有查询到,则插入数据
            {

                //插入衣服表
                SQLiteCommand cmdupdate = conn.CreateCommand();
                cmdupdate.CommandText = "insert into yf(tm,mc,cm,ys) values('" + tm + "','" + mc + "','" + cm + "','" + ys + "')";
                cmdupdate.ExecuteNonQuery();

                //插入售价表
                SQLiteCommand cmdsj = conn.CreateCommand();
                cmdsj.CommandText = "insert into  jg (tm,cb,dj) values ('"+tm+"','"+cb+"','"+sj+"')"; 
                cmdsj.ExecuteNonQuery();

                //插入库存表
                SQLiteCommand cmdkc = conn.CreateCommand();
                cmdkc.CommandText = "insert into kc values('"+tm+"','"+sl+"')";  
                cmdkc.ExecuteNonQuery();

                //插入入库表
                SQLiteCommand cmdrk = conn.CreateCommand();
                cmdrk.CommandText = "insert into rk(tm,rkl,rksj) values ('" + tm + "','" + sl + "','" + DateTime.Now.ToString("yyyyMMdd") + "')";
                cmdrk.ExecuteNonQuery();

                
                MessageBox.Show("新入库成功", "新入库");
            }
            try
            {
                conn.Close();
            }
            catch
            {
                MessageBox.Show("无法正常关闭数据库");
            }

        }


无语了,第一次执行时正常的,但是第二次执行就会出现database is locked
求解~
------解决思路----------------------
不是没有关闭,是dataReader这个对象是独占连接,并且只能向前读
所以你后面update时必须重新new一个conn
using(readercheck)
if (readercheck.HasRows)  //如果有条码,则update数据
            {
                using(conn = new Conn())
               {
                SQLiteCommand cmdupdate = conn.CreateCommand();
                ...
                }
}
  相关解决方案