错误:更新数据库时“没有为一个或多个必需参数指定值”

时间:2021-09-07 17:33:57

I am currently working with a small program where a field must be selected of a DBGrid and then with the help of an inputbox, the value can be updated.

我目前正在使用一个小程序,其中必须从DBGrid中选择一个字段,然后在输入框的帮助下,可以更新该值。

The Database has a field called CaseNumber that has the primary key. The code must identify the field name of the field and the case number of the selected row, with that information, the database can be updated.

数据库有一个名为CaseNumber的字段,其中包含主键。代码必须标识字段的字段名称和所选行的案例编号,使用该信息,可以更新数据库。

Here is a copy of my code:

这是我的代码的副本:

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);

Each time I run it, it gives me an error:

每次运行它都会给我一个错误:

No value given for one or more required parameters.

没有给出一个或多个必需参数的值。

What can I do to solve this?

我该怎么做才能解决这个问题?

1 个解决方案

#1


0  

@Kobik's answer is the correct one for the point you specifically asked about.

@ Kobik的回答是你特别提出的问题的正确答案。

This is another way to address the point, namely how to update data without having to create Sql UPDATE statements yourself. Try this

这是解决这一问题的另一种方法,即如何在不必自己创建Sql UPDATE语句的情况下更新数据。尝试这个

  1. If you don't already have one, add to your form a DBNavigator connected to the same DataSource as your DBGrid. Keep an eye at runtime on the changing state of its buttons.

    如果您还没有,请将DBNavigator添加到与DBGrid相同的DataSource中。关注其按钮状态的变化,密切注意运行时间。

  2. In your code block which opens the AdoQuery, add the following lines after PatientQuery.Open

    在打开AdoQuery的代码块中,在PatientQuery.Open之后添加以下行

:

  DBGrid1.Options := DBGrid1.Options + [dgEditing];
  DBGrid1.Columns[0].ReadOnly := True;

The above assumes your PK is displayed in the LH column of the grid, hence the index 0.

以上假设您的PK显示在网格的LH列中,因此索引为0。

The option dgEditing allows you to click in any cell (except the PK ones, because we've set those to ReadOnly) and change the value. Notice that when you do this at runtime, the tick and cross buttons on the DBNavigator become enabled so that you can save or abandon the change(s). Notice also that if you click in another row while you're editing a cell, the change you've made is automatically posted to the dataset.

选项dgEditing允许您单击任何单元格(除了PK单元,因为我们将它们设置为ReadOnly)并更改值。请注意,在运行时执行此操作时,DBNavigator上的勾选和交叉按钮将变为启用状态,以便您可以保存或放弃更改。另请注意,如果在编辑单元格时单击另一行,则所做的更改将自动发布到数据集。

And, at its simplest, that's all there is to it. On the same Component Palette tab as the DBGrid, you'll also find various other db-aware component that you can use in a similar way, in particular the TDBEdit and TDBMemo.

而且,就其最简单而言,就是它的全部内容。在与DBGrid相同的Component Palette选项卡上,您还可以找到各种其他可以以类似方式使用的db-aware组件,特别是TDBEdit和TDBMemo。

If you need any help about this way of working (which is the standard Delphi way of editing DB data), try googling a bit and, if you're still stuck, ask a new q here on SO.

如果您需要有关这种工作方式的任何帮助(这是编辑DB数据的标准Delphi方式),请尝试使用Google搜索,如果您仍然卡住,请在SO上询问新q。

#1


0  

@Kobik's answer is the correct one for the point you specifically asked about.

@ Kobik的回答是你特别提出的问题的正确答案。

This is another way to address the point, namely how to update data without having to create Sql UPDATE statements yourself. Try this

这是解决这一问题的另一种方法,即如何在不必自己创建Sql UPDATE语句的情况下更新数据。尝试这个

  1. If you don't already have one, add to your form a DBNavigator connected to the same DataSource as your DBGrid. Keep an eye at runtime on the changing state of its buttons.

    如果您还没有,请将DBNavigator添加到与DBGrid相同的DataSource中。关注其按钮状态的变化,密切注意运行时间。

  2. In your code block which opens the AdoQuery, add the following lines after PatientQuery.Open

    在打开AdoQuery的代码块中,在PatientQuery.Open之后添加以下行

:

  DBGrid1.Options := DBGrid1.Options + [dgEditing];
  DBGrid1.Columns[0].ReadOnly := True;

The above assumes your PK is displayed in the LH column of the grid, hence the index 0.

以上假设您的PK显示在网格的LH列中,因此索引为0。

The option dgEditing allows you to click in any cell (except the PK ones, because we've set those to ReadOnly) and change the value. Notice that when you do this at runtime, the tick and cross buttons on the DBNavigator become enabled so that you can save or abandon the change(s). Notice also that if you click in another row while you're editing a cell, the change you've made is automatically posted to the dataset.

选项dgEditing允许您单击任何单元格(除了PK单元,因为我们将它们设置为ReadOnly)并更改值。请注意,在运行时执行此操作时,DBNavigator上的勾选和交叉按钮将变为启用状态,以便您可以保存或放弃更改。另请注意,如果在编辑单元格时单击另一行,则所做的更改将自动发布到数据集。

And, at its simplest, that's all there is to it. On the same Component Palette tab as the DBGrid, you'll also find various other db-aware component that you can use in a similar way, in particular the TDBEdit and TDBMemo.

而且,就其最简单而言,就是它的全部内容。在与DBGrid相同的Component Palette选项卡上,您还可以找到各种其他可以以类似方式使用的db-aware组件,特别是TDBEdit和TDBMemo。

If you need any help about this way of working (which is the standard Delphi way of editing DB data), try googling a bit and, if you're still stuck, ask a new q here on SO.

如果您需要有关这种工作方式的任何帮助(这是编辑DB数据的标准Delphi方式),请尝试使用Google搜索,如果您仍然卡住,请在SO上询问新q。