2013-12-11 32 views
1

我想檢查一個名爲isSolved的表項的布爾型字段。然後編寫一個依賴於布爾值運行代碼的if條件。檢查SQLite表項字段 - C#WPF

我已經試過這種方法:

foreach (int level in builtInLevels2) 
    { 
     myImage = new Image(); 
     int levelNowInt = level; 
     string levelName = "Level " + level; 
     SQLiteConnection sqlConnection; 
     SQLiteCommand sqlite_cmd; 
     try 
     { 
      // create a new database connection: 
      sqlConnection = new SQLiteConnection(dbConnectionString); 

      // open the connection: 
      sqlConnection.Open(); 

      // create a new SQL command: 

      sqlite_cmd = sqlConnection.CreateCommand(); 
      sqlite_cmd.CommandText = "select isSolved from Level where name = " + "'" + levelName + "'" + ";"; 
      var dbEntry = sqlite_cmd.ExecuteNonQuery(); 
      MessageBox.Show(dbEntry.ToString()); 
     } 

VAR dbEntry始終是0。然而,這不是一個布爾值,爲表項的布爾值在某些情況下,實際上1。我如何正確地做到這一點?

回答

1

嘗試,而不是執行以下操作:

var isSolved = (bool)sqlite_cmd.ExecuteScalar(); 

從文檔ExecuteScalar

使用ExecuteScalar方法從數據庫中檢索單個值。

從文檔ExecuteNonQuery

對於所有其他類型的語句(不是UPDATE,INSERT等,DELETE),返回值是-1。如果發生回滾,返回值也是-1。

鑑於文件,我希望你會得到一個-1,而不是0

+0

謝謝,它的工作原理。 – user2602079