多线程插入数据是,经常遇到database is locked情况,很烦人。网上找了很多资料都没有解决。
附上我写的部分代码:
public static string connectionString = @"Data Source=E:\develop\2011-7\BuyEr\BuyEr.db";
public static object GetSingle(string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SQLite.SQLiteException e)
{
connection.Close();
throw new Exception(e.Message);
}
}
}
}
希望路过大侠,能够给出完美的解决方案
最好能够附上代码.
添加锁
private static readonly object _lock = new object();
lock (_lock)
{
// to do something ...
}