這裏是我使用的代碼:接受「數據庫被鎖定」錯誤使用SQLite和C#
public bool save(string name, string type, string city, string org)
{
BLL.maxkey maxkey = new BLL.maxkey();
int maxid = maxkey.getMaxKey(1);
{
SQLiteConnection sqConnection = new SQLiteConnection(@"Data Source= C:\Users\BigDaddyDuergar\Documents\Visual Studio 2012\Projects\MET Character Manager\MET Character Manager\METCMDB");
sqConnection.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = sqConnection;
SQLiteTransaction trans;
trans = sqConnection.BeginTransaction();
cmd.Transaction = trans;
int rows = 0;
try
{
cmd.CommandText = "insert into game values(@gameid, @gamename, @gametype, @gamecity, @gameorg)";
cmd.Parameters.AddWithValue("@gameid", maxid);
cmd.Parameters.AddWithValue("@gamename", name);
cmd.Parameters.AddWithValue("@gametype", type);
cmd.Parameters.AddWithValue("@gamecity", city);
cmd.Parameters.AddWithValue("@gameorg", org);
cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
error error = new error(ex);
}
finally
{
sqConnection.Close();
}
if (rows == 1)
{
return true;
}
else
{
return false;
}
}
}
而且也:
public void updateMaxKey(int tablenum)
{
//using (var conn = new SQLiteConnection(@"Data Source= C:\Users\BigDaddyDuergar\Documents\Visual Studio 2012\Projects\MET Character Manager\MET Character Manager\METCMDB"))
//using (var cmd = conn.CreateCommand())
{
SQLiteConnection sqConnection = new SQLiteConnection(@"Data Source= C:\Users\BigDaddyDuergar\Documents\Visual Studio 2012\Projects\MET Character Manager\MET Character Manager\METCMDB");
sqConnection.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = sqConnection;
SQLiteTransaction trans;
trans = sqConnection.BeginTransaction();
cmd.Transaction = trans;
try
{
cmd.CommandText = "update maxnumkey set maxnum = (maxnum + 1) where tableid = @tableid";
cmd.Parameters.AddWithValue("@tableid", tablenum);
cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
error error = new error(ex);
}
finally
{
sqConnection.Close();
}
}
}
public int returnMaxKey(int tablenum)
{
int maxrows = 0;
{
SQLiteConnection sqConnection = new SQLiteConnection(@"Data Source= C:\Users\BigDaddyDuergar\Documents\Visual Studio 2012\Projects\MET Character Manager\MET Character Manager\METCMDB");
sqConnection.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = sqConnection;
SQLiteTransaction trans;
trans = sqConnection.BeginTransaction();
cmd.Transaction = trans;
try
{
cmd.CommandText = "select maxnum from maxnumkey where tableid = @tableid";
cmd.Parameters.AddWithValue("@tableid", tablenum);
SQLiteDataReader sqReader = cmd.ExecuteReader();
sqReader.Read();
maxrows = sqReader.GetInt32(0);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
error error = new error(ex);
}
finally
{
sqConnection.Close();
}
if (maxrows > 0)
{
return maxrows;
}
else
{
return -1;
}
}
}
我可以運行updatemaxkey功能,其次是returnmaxkey函數沒有錯誤。我在數據庫中驗證正確處理了更新,並且返回的信息匹配。我試圖進入保存功能,並在trans.Commit()我得到一個「數據庫被鎖定」的錯誤。
我似乎無法找到任何闡述此問題的地方,並給出瞭解決此問題的方向。
如果您需要任何進一步的澄清只是問。我剛開始使用大約12小時前的sqlite,所以我希望這是簡單的,我只是俯瞰。
謝謝!
我更新了代碼以利用下面建議的「using」命令,現在在使用塊之後有gc.collect(),而且我仍然遇到問題。不知道如何獲得數據庫文件的版本。 –
好吧,我發現我的問題。我打開閱讀器並開始閱讀數據,並從未關閉它。其他一切都很好。 –