日期:2014-05-18  浏览次数:20876 次

求教 sqlite 事务问题
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection();

  conn.ConnectionString = "Data Source=d:/demo.db3";
  conn.Open();
  string sql = "INSERT INTO Test3(Name,TypeName,addDate)values(@Name,@TypeName,@addDate)";
  SQLiteDBHelper db = new SQLiteDBHelper("D://Demo.db3");
  DbTransaction trans = conn.BeginTransaction();
  for (int c = 0; c <= 2; c++)
  {
  Random i = new Random();
  Random t = new Random();
  SQLiteParameter[] parameters = new SQLiteParameter[]{  
new SQLiteParameter("@Name",i.Next(1,1000)),  
new SQLiteParameter("@TypeName",c),
  new SQLiteParameter("@addDate",t.Next(1,1000))};
  db.ExecuteNonQuery(sql, parameters);
  }
  trans.commit();
错误代码:db.ExecuteNonQuery(sql, parameters);
错误原因:the database file is locked

------解决方案--------------------
估计是并发冲突,检查你的SQLiteDBHelper,确保数据连接用完关闭。
------解决方案--------------------
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection();

conn.ConnectionString = "Data Source=d:/demo.db3";
conn.Open();

提取一个方法出来
using(var conn= GetConn())
{
...
}