使用delphi完成一个数据更新页面
时间: 2024-05-19 13:13:21 浏览: 13
以下是一个使用Delphi的示例代码,用于更新数据库中的数据:
unit UpdateForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, DB, ADODB;
type
TfrmUpdate = class(TForm)
lblTitle: TLabel;
pnlContent: TPanel;
lblName: TLabel;
edtName: TEdit;
lblAge: TLabel;
edtAge: TEdit;
btnSave: TButton;
btnCancel: TButton;
lblID: TLabel;
edtID: TEdit;
qUpdate: TADOQuery;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnCancelClick(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
private
{ Private declarations }
FConnection: TADOConnection;
public
{ Public declarations }
constructor Create(AOwner: TComponent; Connection: TADOConnection; ID: Integer; Name: string; Age: Integer); reintroduce;
end;
var
frmUpdate: TfrmUpdate;
implementation
{$R *.dfm}
constructor TfrmUpdate.Create(AOwner: TComponent; Connection: TADOConnection; ID: Integer; Name: string; Age: Integer);
begin
inherited Create(AOwner);
FConnection := Connection;
edtID.Text := IntToStr(ID);
edtName.Text := Name;
edtAge.Text := IntToStr(Age);
end;
procedure TfrmUpdate.btnCancelClick(Sender: TObject);
begin
Close;
end;
procedure TfrmUpdate.btnSaveClick(Sender: TObject);
begin
qUpdate := TADOQuery.Create(nil);
try
qUpdate.Connection := FConnection;
qUpdate.SQL.Text := 'UPDATE tblPerson SET Name=:Name, Age=:Age WHERE ID=:ID';
qUpdate.Parameters.ParamByName('Name').Value := edtName.Text;
qUpdate.Parameters.ParamByName('Age').Value := StrToIntDef(edtAge.Text, 0);
qUpdate.Parameters.ParamByName('ID').Value := StrToIntDef(edtID.Text, 0);
qUpdate.ExecSQL;
ModalResult := mrOK;
finally
qUpdate.Free;
end;
end;
procedure TfrmUpdate.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
end.
在这个示例中,我们使用了一个ADOQuery组件来更新数据库中的数据。该组件使用连接对象FConnection连接到数据库,然后执行SQL语句来更新tblPerson表中的数据。我们在构造函数中传递了连接对象、ID、名称和年龄参数。在保存按钮的单击事件中,我们将这些值与SQL语句中的参数进行匹配,并使用ExecSQL方法来执行更新。如果更新成功,则设置ModalResult为mrOK,以便在主窗体中处理更新结果。最后,我们使用FormClose事件来释放窗体的资源。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)