如何避免sqlite中的数据库锁定错误 [英] How to avoid Database locked error in sqlite

查看:129
本文介绍了如何避免sqlite中的数据库锁定错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试将记录插入表中但在sqlite中遇到数据库锁定错误。我在vb.net 2010中编写代码并使用sqlite3.0作为数据库。

Hi
I am trying to insert a record into a table but getting database locked error in sqlite. I am writing my code in vb.net 2010 and using sqlite3.0 as database.

If Conn.State = ConnectionState.Open Then Conn.Close()
            Conn.Open()
            q1Var = "insert into mstrs(Code,Name,Vil,Adrs,Pin,type,stts) values(" & ID & ",'" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','d',1)"
            Dim cmd1 As New SQLiteCommand(q1Var, Conn)
            cmd1.ExecuteNonQuery()
            If Conn.State = ConnectionState.Open Then Conn.Close()





提前致谢

Santosh Sharma



Thanks in advance
Santosh Sharma

推荐答案

对所有人抱歉...



sqlite还需要Datareader在尝试将连接用于其他任务之前关闭。



我使用连接进行选择查询,其中我关闭连接而不关闭阅读器。



以下代码修改(标记为粗体和下划线)解决了我的问题



Sorry to all...

sqlite needs the Datareader also to be closed before trying to use the connection for other task.

I had used the connection for a select query in which I closed connection without closing reader.

the below code modification (Marked in Bold and Underlined) solved my problem

If Conn.State = ConnectionState.Open Then Conn.Close()
       Conn.Open()
       Dim ID As Integer = 0
       Dim cmdDis As New SQLiteCommand("select coalesce(Max(Code),0) from mstrs where type='d'", Conn)
       Dim Display As SQLiteDataReader = cmdDis.ExecuteReader()
       If Display.HasRows Then
           //do the stuff here......
       End If
       cmdDis.Dispose()
       Display.Close()
       If Conn.State = ConnectionState.Open Then Conn.Close()
   End Sub





问候

Santosh Sharma



Regards
Santosh Sharma


这篇关于如何避免sqlite中的数据库锁定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆