2014-03-13 56 views
1

這裏是我使用的代碼:接受「數據庫被鎖定」錯誤使用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,所以我希望這是簡單的,我只是俯瞰。

謝謝!

+0

我更新了代碼以利用下面建議的「using」命令,現在在使用塊之後有gc.collect(),而且我仍然遇到問題。不知道如何獲得數據庫文件的版本。 –

+0

好吧,我發現我的問題。我打開閱讀器並開始閱讀數據,並從未關閉它。其他一切都很好。 –

回答

1

Basic .NET中:

  • 處置一次性的對象。命令,連接都是這樣的。

  • 這是最簡單的使用「使用」語句。殺死你的一半的代碼。

  • 這對交易是非常重要的。

我只能打賭,SqlConncetion - 由於違反要求您dipose實現IDisposable接口對象的基本原則.NET - 不釋放的情況下的文件共享你重新打開以及 - 塊數據庫(它不能很好地處理多個並行訪問)。

沒有什麼特別的SqlCe - 你的C#代碼違反了核心.NET原則。

+0

好吧,我正在使用使用命令,但我不再。我有點困惑於此。爲了解決這個問題,我需要更改或查看哪些內容? –