2013-12-11 27 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