FireDAC 查询连接到 DBGrid

FireDAC Query connect to DBGrid

我有 FDQuery、DataSource 和 DBGrid。现在我在 Button

中写这段代码
  FDQuery2->Active = false;
  FDQuery2->SQL->Clear();
  FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
                  " FROM Checkup C "
                  " INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
                  " WHERE C.today = " + MaskEdit1->Text +
                  " ORDER BY C.check_id ");
  FDQuery2->Active = true;

我将 FDQuery 连接到 DataSource,将 tDataSource 连接到 DBGrid,但是当我单击按钮时,它不显示行。而且我确信 SQL 代码是有效的,因为当我在 SQL 字符串中写入时,行已经显示。 任何想法。

您在连接文本时缺少围绕您的值的 '。更改您的 WHERE 子句:

FDQuery2->SQL->Clear();
FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
                " FROM Checkup C "
                " INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
                " WHERE C.today = '" + MaskEdit1->Text + "'" +
                " ORDER BY C.check_id ");

不过,您确实应该学会使用参数化查询。它允许数据库驱动程序为您处理诸如正确引用文本或格式化日期之类的事情,并且它还(重要地)防止 SQL 注入。

FDQuery2->SQL->Clear();
FDQuery2->SQL->Add(" SELECT C.patient_id, P.patient_name, C.check_id "
                " FROM Checkup C "
                " INNER JOIN Patient P ON (C.patient_id=P.patient_id) "
                " WHERE C.today = :today" + 
                " ORDER BY C.check_id ");
FDQuery2->ParamByName("today")->AsString = MaskEdit1.Text;
FDQuery2->Active = true;