当前位置:网站首页>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
边栏推荐
- DDD based on ABP -- Entity creation and update
- Class loading mechanism and parental delegation mechanism
- Fraud detection cases and Titanic rescued cases
- 24.原生磁盘的使用
- Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection
- DS diagram - the shortest path of the diagram (excluding the code framework)
- Summary of Baimian machine learning
- DS binary tree - parent and child nodes of binary tree
- spark学习笔记(三)——sparkcore基础知识
- 佣金哪家券商最低,我要开户,手机上开户安不安全
猜你喜欢

Unity 使用NVIDIA FleX for Unity插件实现制作软体、水流流体、布料等效果学习教程

Simple understanding and implementation of unity delegate

【OpenCV 例程300篇】238. OpenCV 中的 Harris 角点检测

Learning rate adjustment strategy in deep learning (1)
![Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022](/img/fd/c7bff1ce199e8b600761d77828c674.png)
Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022

Various searches (⊙▽⊙) consolidate the chapter of promotion

Meaning of 7 parameters of thread pool

(09) flask is OK if it has hands - cookies and sessions

Kotlin类与继承

打假Yolov7的精度,不是所有的论文都是真实可信
随机推荐
Google Earth Engine——使用MODIS数据进行逐月数据的过火(火灾)面积并导出
Data analysis and mining 1
zabbix管理员忘记登录密码
Rasa 3.x learning series -rasa fallbackclassifier source code learning notes
野火stm32霸道,通过固件库实现流水灯
Performance test - Test Execution
(零九)Flask有手就行——Cookie和Session
The first n rows sorted after dataframe grouping nlargest argmax idmax tail!!!!
Machine learning practice notes
DS inner row heap sort
Comparison of traversal speed between map and list
DDD based on ABP -- Entity creation and update
打假Yolov7的精度,不是所有的论文都是真实可信
A common Dao class and util
Preparation of mobile end test cases
Can you buy 6% of financial products after opening a stock account?
Meaning of 7 parameters of thread pool
Number of bytes occupied by variables of type char short int in memory
Which securities company is good at opening an account with flush? Excuse me, is it safe to open an account with mobile phone or stock?
深入浅出边缘云 | 2. 架构