当前位置:网站首页>C# SQLite Database Locked exception
C# SQLite Database Locked exception
2022-07-24 15:08:00 【ITKEY_】
现象
在查询时没有问题,但是在Insert时会报数据库被锁定。
原因分析
可能是代码某个地方连接着数据库,忘记关闭了。
解决办法
原理我还没有完全搞懂,不过根据下面的写法确实解决了问题。
在某个地方,连接处于打开状态。摆脱OpenConnection和CloseConnection改变ExecuteNonQuery这个:
using (SQLiteConnection c = new SQLiteConnection(ConnectionString))
{
c.Open();
using (SQLiteCommand cmd = new SQLiteCommand(sql, c))
{
cmd.ExecuteNonQuery();
}
}
此外,将读取数据的方式更改为:
using (SQLiteConnection c = new SQLiteConnection(ConnectionString))
{
c.Open();
using (SQLiteCommand cmd = new SQLiteCommand(sql, c))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
...
}
}
}
不要尝试像在这里一样自行管理连接池。首先,它比您编写的代码要复杂得多,但其次,它已经在SQLiteConnection对象内部进行了处理。最后,如果你没有利用using,你就没有正确地处理这些对象,你最终会遇到像你现在看到的那样的问题。
参考
https://stackoverflow.com/questions/17592671/sqlite-database-locked-exception
边栏推荐
- 25.从生磁盘到文件
- Vector introduction and underlying principle
- Preparation of mobile end test cases
- 文件操作详解
- 循环结构practice
- DS diagram - the shortest path of the diagram (excluding the code framework)
- Problems needing attention in mobile terminal testing
- The first n rows sorted after dataframe grouping nlargest argmax idmax tail!!!!
- Here comes the problem! Unplug the network cable for a few seconds and plug it back in. Does the original TCP connection still exist?
- Number of bytes occupied by variables of type char short int in memory
猜你喜欢

关于构建网络安全知识库方向相关知识的学习和思考

老虎口瀑布:铜梁版小壶口瀑布

LeetCode·每日一题·1184.公交站间的距离·模拟

onBlur和onChange冲突解决方法

《Route planning method for UAV in unknown environment based on improved SAS algorithm》翻译

Huawei camera capability

Performance test - Test Execution

Deep learning 1 perceptron and implementation of simple back propagation network

spark学习笔记(三)——sparkcore基础知识

zabbix管理员忘记登录密码
随机推荐
Kotlin class and inheritance
Outlook tutorial, how to create tasks and to DOS in outlook?
ZABBIX administrator forgot login password
Spark: get the access volume of each time period in the log (entry level - simple implementation)
Operation of MySQL Library
Property datasource is required exception handling [idea]
Simple encapsulation of wechat applet wx.request
Detailed explanation of IO model (easy to understand)
Extjs4 instance address and Chinese document address
Rest style
Is it safe for Huatai Securities to open an account? Can it be handled on the mobile phone?
Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022
Video game design report template and resources over the years
25.从生磁盘到文件
CSDN garbage has no bottom line!
JS data transformation -- Transformation of tree structure and tile structure
Clear all spaces in the string
使用 Fiddler Hook 报错:502 Fiddler - Connection Failed
kali简洁转换语言方法(图解)
pip换源