【发布时间】:2016-08-30 10:55:09
【问题描述】:
我目前正在使用一个小程序,其中必须从 DBGrid 中选择一个字段,然后在输入框的帮助下,可以更新值。
数据库有一个名为CaseNumber 的字段,它具有主键。代码必须识别字段的字段名称和所选行的案例编号,使用该信息可以更新数据库。
这是我的代码的副本:
procedure TfrmManagePatientInformation.btnChangeClick(Sender: TObject);
var NewValue, Fieldname : String;
CaseNumber : Integer;
begin
// Make sure that the Case Number will not be changed
if DBGridPatients.SelectedField.FieldName = 'CaseNumber' then
begin
MessageDlg('The Case Number of a patient cannot be changed!',mtError,[mbOK],0);
Exit;
end
else
begin
// Get Fieldname
Fieldname := DBGridPatients.SelectedField.FieldName;
// Get Case Number of the selected row
CaseNumber := StrToInt(DBGridPatients.Fields[0].Value);
// Get new value
NewValue := InputBox('New Value','Please enter the new value!','');
// Change the value permanently
PatientQuery.Active := False;
PatientQuery.SQL.Text := 'Update CurrentPatients SET ' + QuotedStr(FieldName) + ' = ' + QuotedStr(NewValue) + ' where CaseNumber = :CaseNumber';
PatientQuery.Parameters.ParamByName('CaseNumber').Value := CaseNumber;
PatientQuery.ExecSQL;
MessageDlg('Information changed successfully!',mtConfirmation,[mbOK],0);
每次运行它都会给我一个错误:
没有为一个或多个必需参数指定值。
我能做些什么来解决这个问题?
【问题讨论】:
-
在执行前
PatientQuery.SQL.Text是什么?为什么不将NewValue也设为命名参数? -
PatientQuery 是什么组件?它是 AdoQuery 吗?如果是这样,请检查执行选项它应该是空的
-
@GuidoG 是的,它是一个 AdoQuery,但它是空的
-
FieldName 不应该有 QuotedStr。
-
@Zenyl 然后检查 PatientQuery.ExecuteOption = []