45 .net c# sqlite ado.net system.data.sqlite
我正在从SQLite获取数据库锁定异常仅用于某些查询.
下面是我的代码:当我执行任何select语句时,它工作正常.
当我在JobsTable 上执行任何写语句时,它也可以正常工作.
这很好用:
ExecuteNonQuery("DELETE FROM Jobs WHERE id=1");
Run Code Online (Sandbox Code Playgroud)
但是,如果我正在执行对Employees表的查询,它会抛出数据库被锁定的异常.
抛出异常:
ExecuteNonQuery("DELETE FROM Employees WHERE id=1");
Run Code Online (Sandbox Code Playgroud)
以下是我的功能:
public bool OpenConnection()
{
if (Con == null)
{
Con = new SQLiteConnection(ConnectionString);
}
if (Con.State == ConnectionState.Closed)
{
Con.Open();
//Cmd = new SQLiteCommand("PRAGMA FOREIGN_KEYS=ON", Con);
//Cmd.ExecuteNonQuery();
//Cmd.Dispose();
//Cmd=null;
return true;
}
if (IsConnectionBusy())
{
Msg.Log(new Exception("Connection busy"));
}
return false;
}
public Boolean CloseConnection()
{
if (Con != null && Con.State == ConnectionState.Open)
{
if (Cmd != null) Cmd.Dispose();
Cmd = null;
Con.Close();
return true;
}
return false;
}
public Boolean ExecuteNonQuery(string sql)
{
if (sql == null) return false;
try
{
if (!OpenConnection())
return false;
else
{
//Tx = Con.BeginTransaction(IsolationLevel.ReadCommitted);
Cmd = new SQLiteCommand(sql, Con);
Cmd.ExecuteNonQuery();
//Tx.Commit();
return true;
}
}
catch (Exception exception)
{
//Tx.Rollback();
Msg.Log(exception);
return false;
}
finally
{
CloseConnection();
}
}
Run Code Online (Sandbox Code Playgroud)
这是例外:在第103行: Cmd.ExecuteNonQuery();
发现异常:类型:System.Data.SQLite.SQLiteException消息:数据库已锁定数据库已锁定源:System.Data.SQLite
Stacktrace:System.Data上的System.Data.SQLite.SQLiteDataReader.NextResult()的System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt),System.Data的System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd,CommandBehavior behavior)位于D:\ Projects\C#Applications\Completed Projects\TimeSheet6\TimeSheet6\DbOp中TimeSheet6.DbOp.ExecuteNonQuery(String sql)的System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()处的.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior行为). cs:103行
Mik*_*oud 87
在某个地方,连接处于打开状态.摆脱OpenConnection与CloseConnection和改变ExecuteNonQuery这样的:
using (SQLiteConnection c = new SQLiteConnection(ConnectionString))
{
c.Open();
using (SQLiteCommand cmd = new SQLiteCommand(sql, c))
{
cmd.ExecuteNonQuery();
}
}
Run Code Online (Sandbox Code Playgroud)
此外,将您读取数据的方式更改为:
using (SQLiteConnection c = new SQLiteConnection(ConnectionString))
{
c.Open();
using (SQLiteCommand cmd = new SQLiteCommand(sql, c))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
请勿尝试自己管理连接池,就像您在这里一样.首先,它比你编码的要复杂得多,但其次,它已经在SQLiteConnection对象内处理了.最后,如果你没有利用using,你就不会妥善处理这些物品,最终会出现你现在所看到的问题.
Dam*_*ith 14
您可以使用如下所示的'using'语句,这将确保连接和命令即使在异常中也能正确处理
private static void ExecuteNonQuery(string queryString)
{
using (var connection = new SQLiteConnection(
ConnectionString))
{
using (var command = new SQLiteCommand(queryString, connection))
{
command.Connection.Open();
command.ExecuteNonQuery();
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
在尝试将任何数据写入数据库之前,您应该关闭 DataReader。用:
dr.Close();
Run Code Online (Sandbox Code Playgroud)
使用完 DataReader 后。
| 归档时间: |
|
| 查看次数: |
57992 次 |
| 最近记录: |