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.

相关阅读 >>

Delphi 实现数据库读取图片文件

Delphi fmx getapppath 获取app路径

Delphi 2009 泛型容器单元(generics.collections)[2]: tqueue<t>

Delphi版雪花算法

Delphi 播放背景音乐

Delphi中unicode转中文

Delphi xe3里判断网络是否连接成功

Delphi 计算运行耗时的方法1

Delphi中禁用和取消android应用睡眠

Delphi 获取当前进程的父进程

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...