【问题标题】:Error comparing values in "if" construct比较“if”构造中的值时出错
【发布时间】:2015-09-08 12:45:25
【问题描述】:

我正在做一个人脸识别项目,我想将x.label 返回的值与保存到数据库中的ID 进行比较。 在将 ID 与 x.label 进行比较时,if 条件下总是出错

  public int Predict(Image<Gray, Byte> testImage)
        {

            Emgu.CV.FaceRecognizer.PredictionResult x;
            if (testImage != null)
            {
                x= fr.Predict(testImage);
                pictureBox1.Image = testImage.ToBitmap();
                //MessageBox.Show(x.Label.ToString()+" "+x.Distance.ToString());

                OleDbCommand D1 = new OleDbCommand("Select [ID] FROM [connect]", DBConnection);
                OleDbDataReader reader = D1.ExecuteReader();
                while (reader.Read())
                {

                   if (Convert.ToInt32(reader["ID"]) == x.Label)
                   {

                       OleDbCommand insert1 = new OleDbCommand("UPDATE OutputReport SET [Attendance]='yes' WHERE [StudentID]='109';", DBConnection);
                       insert1.ExecuteNonQuery();
                        MessageBox.Show(" its working");
                    }
               }
                RefreshDBConnection();


                if (x.Distance < 69)
                    return x.Label;
                else
                    MessageBox.Show("Error!!, The detected Face is neither recognised nor enrolled");
                return -1;
            }

            else

            {

                return -1;
            }

        }

【问题讨论】:

  • ...您总是遇到什么错误?
  • 错误 1 ​​当前上下文中不存在名称“ID”
  • @mariam 这个ID是来自数据库吗??
  • 该代码几乎无法满足您的需求。由于else 部分没有块,因此只有MessageBox.Showelse 块的一部分,因此您将始终返回-1。另外,你还没有声明D1,你只是想给它一个值。

标签: c# database ms-access if-statement face-recognition


【解决方案1】:

如果我的理解是正确的,您需要检查 MS-Access 数据库中的 x.Label 值和 ID 列值。为此,您需要执行 reader 并分配值。

   public int Predict(Image<Gray, Byte> testImage)
        {

            Emgu.CV.FaceRecognizer.PredictionResult x;
            if (testImage != null)
            {
                x= fr.Predict(testImage);
                pictureBox1.Image = testImage.ToBitmap();
                //MessageBox.Show(x.Label.ToString()+" "+x.Distance.ToString());

                OleDbCommand D1 = new OleDbCommand("Select [ID] FROM [connect]", DBConnection);
                OleDbDataReader reader = D1.ExecuteReader();
                while (reader.Read())
                {

                   if (Convert.ToInt32(reader["ID"]) == x.Label)
                   {

                       OleDbCommand insert1 = new OleDbCommand("UPDATE OutputReport SET [Attendance]='yes' WHERE [StudentID]='109';", DBConnection);
                      int i= insert1.ExecuteNonQuery();
                      MessageBox.Show(i.ToString());
                   }
               }
                RefreshDBConnection();

                if (x.Distance < 69)
                    return x.Label;
                else
                    MessageBox.Show("Error!!, The detected Face is neither recognised nor enrolled");
                return -1;
            }

        else

        {

            return -1;
        }


    }

【讨论】:

  • 非常感谢您的建议,但我仍然有一个错误错误 1 ​​运算符 '==' 不能应用于 if (reader["ID "]==x.Label)
  • 如果 x.Label 是一个 int,那么我认为您最好将比较保留为整数,而不是字符串:if (Convert.ToInt64(reader["ID"]) == x.Label)
  • 非常感谢您的回复,但它没有给出任何错误但仍然没有自动填充数据库列
  • @mariam 你的意思是yes 没有插入到 outputreprt 表中?
  • @Sachu 不,它没有插入
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 2014-10-23
相关资源
最近更新 更多