⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit3.pas

📁 自己做的一个例子
💻 PAS
字号:
unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, StdCtrls, Buttons;

type
  TForm3 = class(TForm)
    ADOQuery1: TADOQuery;
    BitBtn1: TBitBtn;
    txt_sName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    txt_sPhone: TEdit;
    txt_sID: TEdit;
    Label3: TLabel;
    BitBtn2: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation
Uses unit1;

{$R *.dfm}

procedure TForm3.BitBtn1Click(Sender: TObject);
Var n:Integer;
begin
    With AdoQuery1 do
    Try
        if Not Active then Open;
        if IsEmpty then Exit;
        //
        txt_sID.Text:=FieldValues['sID'];
        txt_sName.Text:=FieldByName('sName').AsString;
        txt_sPhone.Text:= FieldByName('sPhone').Value;
        //
        Close;
    Except
        On X:Exception do ShowMessage('调用数据显示出错!'+#13+X.Message);
    End;
end;

procedure TForm3.BitBtn2Click(Sender: TObject);
begin
    With AdoQuery1 do
    Try
        if txt_sID.Text='' then Raise Exception.Create('编号主键不能是空的!');
        //首先检查是否存在该记录
        Close;
        SQL.Text:='Select * From Employee Where sID='+QuotedStr(txt_sID.Text);
        Open;
        //如果没有该数据
        if IsEmpty then Begin
            SQL.Text:='Insert Into Employee ';
            SQL.Add('  (sID, sName, sPhone) ');
            SQL.Add('Values(:sID, :sName, :sPhone)' );
          End
        Else Begin      //若没有数据
            SQL.Text:='Update Employee Set ';
            SQL.Add(' sName=:sName, sPhone=:sPhone ');
            SQL.Add(' Where sID=:sID');
        End;
        //公共参数
        Parameters.ParamValues['sID']:=txt_sID.Text;
        Parameters.ParamValues['sName']:=txt_sName.Text;    
        Parameters.ParamByName('sPhone').Value:=txt_sPhone.Text;
        //执行
        ExecSQL;
        //完成
        Messagebox(Handle,'完成!','OK',0);
    Except
        On E:Exception do ShowMessage('更新完毕!'+#13+E.Message);
    End;
end;

procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    Action:=caFree;
end;

end.
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -