多线程插入数据是,经常遇到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 ...
}