I\'m using System.Data.SQLite provider in an ASP.NET application (framework 4.0). The issue I\'m running into is that when I INSERT something in a table in the SQLite databa
I was only having the problems mentioned here when locking the computer, even after unlocking it, was working fine otherwise lucky cuz i just started locking it lately and the software just released couple days ago before anyone knows about it.
Anyway i had all the stuff like closing connections and ClearAllPools etc but was missing aTableAdapter.Adapter.Dispose() and that fixed it.
In my case I was creating SQLiteCommand
objects without explicitly disposing them.
var command = connection.CreateCommand();
command.CommandText = commandText;
value = command.ExecuteScalar();
I wrapped my command in a using
statement and it fixed my issue.
static public class SqliteExtensions
{
public static object ExecuteScalar(this SQLiteConnection connection, string commandText)
{
// Added using
using (var command = connection.CreateCommand())
{
command.CommandText = commandText;
return command.ExecuteScalar();
}
}
}
Then you can use it like this
connection.ExecuteScalar(commandText);
In most cases the problem will arise if you don't dispose your readers and commands properly. There is a scenario in which commands and readers will not dispose properly.
Scenario 1: In case you are running a boolean function. before a result is reached the code in the finally block will not excecute. This is a big problem if you are going to be evaluating the results of function isDataExists while executing code if it suits the result i.e
if(isDataExists){
// execute some code
}
The function being evaluated
public bool isDataExists(string sql)
{
try
{
OpenConnection();
SQLiteCommand cmd = new SQLiteCommand(sql, connection);
reader = cmd.ExecuteReader();
if (reader != null && reader.Read())
{
return true;
}
else
{
return false;
}
}
catch (Exception expMsg)
{
//Exception
}
finally
{
if (reader != null)
{
reader.Dispose();
}
CloseConnection();
}
return true;
}
Solution: Dispose your reader and command inside the try block as follows
OpenConnection();
SQLiteCommand cmd = new SQLiteCommand(sql, connection);
reader = cmd.ExecuteReader();
if (reader != null && reader.Read())
{
cmd.Dispose();
CloseConnection();
return true;
}
else
{
cmd.Dispose();
CloseConnection();
return false;
}
Finally dispose the reader and command just in case some thing went wrong
finally
{
if (reader != null)
{
reader.Dispose();
}
CloseConnection();
}
The following worked for me:
MySQLiteConnection.Close();
SQLite.SQLiteConnection.ClearAllPools()
Had the same problem. Solved it simply by installing SQLite 1.0.111 (via nuget).
Did not have to change anything on my code, only update to that version. Previously I used 1.0.85 where the file was locked, although the connection was closed and disposed.
I found edymtt's answer right about blaming TableAdapters / Datasets, but instead of modifying the every time re-generated TableAdapter codefile, I found an other solution: to manually call .Dispose on the TableAdapter's child elements. (In .NET 4.5, latest SQLite 1.0.86)
using (var db = new testDataSet())
{
using (testDataSetTableAdapters.UsersTableAdapter t = new testDataSetTableAdapters.UsersTableAdapter())
{
t.Fill(db.Users);
//One of the following two is enough
t.Connection.Dispose(); //THIS OR
t.Adapter.Dispose(); //THIS LINE MAKES THE DB FREE
}
Console.WriteLine((from x in db.Users select x.Username).Count());
}