数据库被锁定sqCommand.ExecuteNonQuery()?

时间:2017-11-10 23:22:07

标签: c# c#-4.0 sqlite

我有以下代码:

string sql = String.Format("insert into {0}({1}) values({2});", tableName, columns, values);
SQLiteConnection myConn = null;
try 
{
    myConn = new SQLiteConnection(dbConnection);
    myConn.Open();

    using(SQLiteCommand sqCommand = new SQLiteCommand(sql, myConn))
    {
        sqCommand.CommandText = sql;
        int rows = sqCommand.ExecuteNonQuery();
        return !(rows == 0);
    }
} 
finally 
{
    myConn.Close();
}

它总是返回错误:Database is locked行:

int rows = sqCommand.ExecuteNonQuery();

Whu会发生吗?

1 个答案:

答案 0 :(得分:2)

我敢打赌,当sqCommand.ExecuteNonQuery()方法执行时(锁仍然适用于以前的连接),数据库连接在某处保持打开状态,因此您需要通过包装每个 {{1}来重构带有SQLiteConnection语句块的实例,如下所示:

using

参考:

Writing to a database file