问题描述
有一个简单的代码
var insert =
@"INSERT INTO [files] (
[Name],
[FullName],
[MD5])
VALUES (@Name, @FullName, @MD5);";
using (var con = _db.OpenConnection())
{
using (var cmd = con.CreateCommand())
{
cmd.CommandText = insert;
cmd.Parameters.AddWithValue("@Name", item.Name);
cmd.Parameters.AddWithValue("@FullName", item.FullName);
cmd.Parameters.AddWithValue("@MD5", item.MD5);
cmd.ExecuteNonQuery();
}
}
应用程序挂起一段时间
applications hangs for some time when executing
cmd.ExecuteNonQuery();
和再失败,异常数据库被锁定。
为什么发生这种情况?应用程序不是多线程。 DB文件是刚刚创建的。
and then fails with exception "database is locked".Why this is happening? Application is not multithreaded. DB file is just-created.
推荐答案
问题解决了。
此代码之前有呼叫SqlCommand.ExecuteReader()。虽然这创造了这个阅读器连接和命令被处置,这本身读者没有配置。与使用(阅读器)连接被正常关闭和错误上面消失了固定后
Problem solved.Before this code there was call to SqlCommand.ExecuteReader(). Though connection and command which created this reader were disposed, this reader itself wasn't disposed. After fixing with "using(reader)" connection was closed properly and error above disappeared.
在总:DataReader的仍然可以持有,即使连接的SqlCommand被明确设置在连接
In total: DataReader can still hold a connection even if connection and SqlCommand were disposed explicitly.
这篇关于SQLite数据库被锁定在插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!