DBMNG数据库管理与应用

书籍是全世界的营养品。生活里没有书籍,就好像没有阳光;智慧里没有书籍,就好像鸟儿没有翅膀。
当前位置:首页 > 经验分享 > Delphi

Delphi用ADO操作数据库完整示例

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids, StdCtrls, RzDBGrid, ExtCtrls, RzPanel,
ComCtrls, RzBckgnd, RzButton, Buttons, RzCmboBx;

type
TForm1 = class(TForm)
    DataSource1: TDataSource;
    ADOQuery1: TADOQuery;
    Label1: TLabel;
    RzGroupBox1: TRzGroupBox;
    RzDBGrid1: TRzDBGrid;
    RzGroupBox2: TRzGroupBox;
    Label2: TLabel;
    Edit1: TEdit;
    Label3: TLabel;
    Edit2: TEdit;
    Label4: TLabel;
    Edit3: TEdit;
    Label5: TLabel;
    Edit4: TEdit;
    Label6: TLabel;
    Edit5: TEdit;
    RzSeparator1: TRzSeparator;
    StatusBar1: TStatusBar;
    SpeedButton1: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    RzGroupBox3: TRzGroupBox;
    RzComboBox1: TRzComboBox;
    Edit6: TEdit;
    RzButton1: TRzButton;
    Label7: TLabel;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure ShowData;           //记得在此位置加入自定义过程的申明
    procedure Edit6Click(Sender: TObject);
    procedure RzButton1Click(Sender: TObject);
    procedure RzComboBox1Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure SpeedButton7Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);


private
    { Private declarations }
   
public
    { Public declarations }
end;

var
   Form1:   TForm1;

implementation
{$R *.dfm}
procedure TForm1.ShowData;   //自定义过程      如果用DBEdit控件绑定字段就不用这样,
   begin
     Edit1.Text:=ADOQuery1.Fields[0].Value;
     Edit2.Text:=ADOQuery1.Fields[1].Value;
     Edit3.Text:=ADOQuery1.Fields[5].Value;
     edit4.Text:=ADOQuery1.Fields[3].Value;
     edit5.Text:=ADOQuery1.Fields[4].Value;

   end;

procedure TForm1.FormCreate(Sender: TObject);
var
    sqlstr:string;
begin
sqlstr:='select * from abc';                     //数据库连接字符串,打开数据库表,初始化查询
with ADOQuery1 do
begin
    ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='
     +ExtractFilepath(Application.ExeName)+
     'a.MDB;Persist Security Info=True';
    SQL.Clear;
    SQL.Add(sqlstr);
    Open;
end;
ShowData;
StatusBar1.Panels.Items[1].Text:='当前数据库中共有'+''+IntToStr(Form1.ADOQuery1.Recordcount)+''+'条记录';
StatusBar1.Panels.Items[0].Text:='准备就绪!';
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);          //第一条
begin
ADOQuery1.First;
ShowData;
end;

procedure TForm1.SpeedButton3Click(Sender: TObject);         //上一条
begin
ADOQuery1.Prior;
ShowData;
end;

procedure TForm1.SpeedButton4Click(Sender: TObject);        //下一条
begin
ADOQuery1.Next;
ShowData;
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);        //最后一条
begin
ADOQuery1.Last;
ShowData;
end;


procedure TForm1.Edit6Click(Sender: TObject);              //简单判断后激活查询按钮
begin
if RzComboBox1.Text<>'请选择查询字段'then
    begin
      RzButton1.Enabled:=True;
    end
else
    exit;
end;
procedure TForm1.RzButton1Click(Sender: TObject);         //查询按钮处理过程
var
    s:string;
begin

   if (RzComboBox1.Text='根据工号查询')and (Edit6.Text<>'') then
     begin
       s:='Select * from abc where sCardID like''%'+Edit6.Text+'%''';
     end;

   if (RzComboBox1.Text='根据姓名查询')and (Edit6.Text<>'') then
     begin
       s:='Select * from abc where sName like''%'+Edit6.Text+'%''';
     end;

   if (RzComboBox1.Text='根据身份证号码查询')and (Edit6.Text<>'') then
     begin
       s:='Select * from abc where sID like''%'+Edit6.Text+'%''';
     end;

if (RzComboBox1.Text='根据地址查询')and (Edit6.Text<>'') then
    begin
      s:='Select * from abc where sAddress like''%'+Edit6.Text+'%''';
      //showmessage (s);
    end;

with ADOQuery1 do
     begin
     try                     //此处加入错误捕获处理机制
       SQL.Clear;
       SQL.Add(s);
       open;
     except
       MessageBox(Handle,'您选择的查询字段跟输入的值不匹配,请检查后重新输入!'
      +#13+'"例如您选择了根据身份证号码查询就只能在文本框中输入数字,而不能输入字符串"'
      ,'警告信息',MB_ICONEXCLAMATION);
     end;
ShowData;
StatusBar1.Panels.Items[1].Text:='此次查询共有'+''+IntToStr(Form1.ADOQuery1.Recordcount)+''+'条符合条件的记录';
end;
end;
procedure TForm1.RzComboBox1Click(Sender: TObject);
begin
Edit6.Text:='';
end;

procedure TForm1.SpeedButton5Click(Sender: TObject);         //添加空行
begin
with ADOQuery1 do
begin
    Append;
end;
end;

procedure TForm1.SpeedButton6Click(Sender: TObject);       //保存所有记录,
begin
with ADOQuery1 do
begin
    edit;
    post;
end;
end;

procedure TForm1.SpeedButton7Click(Sender: TObject);       //删除记录
begin
ADOQuery1.Delete;
end;

procedure TForm1.Timer1Timer(Sender: TObject);              //状态栏显示时间
begin
StatusBar1.Panels.Items[3].Text:=DateTimeToStr(Now);
end;

end.

 

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号