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

上传人:b****5 文档编号:5930321 上传时间:2023-01-02 格式:DOCX 页数:8 大小:15.77KB
下载 相关 举报
Delphi用ADO操作数据库完整示例.docx_第1页
第1页 / 共8页
Delphi用ADO操作数据库完整示例.docx_第2页
第2页 / 共8页
Delphi用ADO操作数据库完整示例.docx_第3页
第3页 / 共8页
Delphi用ADO操作数据库完整示例.docx_第4页
第4页 / 共8页
Delphi用ADO操作数据库完整示例.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

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

《Delphi用ADO操作数据库完整示例.docx》由会员分享,可在线阅读,更多相关《Delphi用ADO操作数据库完整示例.docx(8页珍藏版)》请在冰豆网上搜索。

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

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

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

转)

unitUnit1;

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;

procedureFormCreate(Sender:

TObject);

procedureSpeedButton1Click(Sender:

TObject);

procedureSpeedButton3Click(Sender:

TObject);

procedureSpeedButton4Click(Sender:

TObject);

procedureSpeedButton2Click(Sender:

TObject);

procedureShowData;//记得在此位置加入自定义过程的申明

procedureEdit6Click(Sender:

TObject);

procedureRzButton1Click(Sender:

TObject);

procedureRzComboBox1Click(Sender:

TObject);

procedureSpeedButton5Click(Sender:

TObject);

procedureSpeedButton6Click(Sender:

TObject);

procedureSpeedButton7Click(Sender:

TObject);

procedureTimer1Timer(Sender:

TObject);

private

{Privatedeclarations}

public

{Publicdeclarations}

end;

var

Form1:

TForm1;

implementation

{$R*.dfm}

procedureTForm1.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;

procedureTForm1.FormCreate(Sender:

TObject);

var

sqlstr:

string;

begin

sqlstr:

='select*fromabc';//数据库连接字符串,打开数据库表,初始化查询

withADOQuery1do

begin

ConnectionString:

='Provider=Microsoft.Jet.OLEDB.4.0;DataSource='+ExtractFilepath(Application.ExeName)+

'a.MDB;PersistSecurityInfo=True';

SQL.Clear;

SQL.Add(sqlstr);

Open;

end;

ShowData;

StatusBar1.Panels.Items[1].Text:

='当前数据库中共有

'+”+1ntToStr(Form1.ADOQuery1.Recordcount)+”+条记录';

StatusBar1.Panels.Items[0].Text:

='准备就绪!

';

end;

procedureTForm1.SpeedButton1Click(Sender:

TObject);//

begin

ADOQuery1.First;

ShowData;

end;

procedureTForm1.SpeedButton3Click(Sender:

TObject);//

begin

ADOQuery1.Prior;

ShowData;

end;

procedureTForm1.SpeedButton4Click(Sender:

TObject);//

begin

ADOQuery1.Next;

ShowData;

end;

procedureTForm1.SpeedButton2Click(Sender:

TObject);//begin

ADOQuerylLast第一条上一条

下一条

最后一条ShowData;

end;

procedureTForm1.Edit6Click(Sender:

TObject);//简单判断后激活查询按钮

begin

ifRzComboBoxl.Textv^选择查询字段'then

begin

RzButton1.Enabled:

=True;

end

else

exit;

end;

procedureTForm1.RzButton1Click(Sender:

TObject);//查询按钮处理过程var

s:

string;

begin

if(RzComboBox1.Text=根据工号查询')and(Edit6.Text<>”)then

begin

s:

='Select*fromabcwheresCardIDlike''%'+Edit6.Text+'%''';end;

if(RzComboBox1.Text=根据姓名查询')and(Edit6.Text<>'')then

begin

s:

='Select*fromabcwheresNamelike''%'+Edit6.Text+'%''';

end;

if(RzComboBox1.Text=根据身份证号码查询')and(Edit6.Text<>'')thenbegin

s:

='Select*fromabcwheresIDlike''%'+Edit6.Text+'%''';

end;

if(RzComboBox1.Text=根据地址查询')and(Edit6.Text<>'')then

begin

s:

='Select*fromabcwheresAddresslike''%'+Edit6.Text+'%''';//showmessage(s);end;

withADOQuery1do

begin

try//此处加入错误捕获处理机制

SQL.Clear;

SQL.Add(s);

open;

except

MessageBox(Handle您选择的查询字段跟输入的值不匹配,请检查后重新输入!

'+#13+'"例如您选择了根据身份证号码查询就只能在文本框中输入数字,而不能输入字符串"'

'警告信息',MB_ICONEXCLAMATION);

end;

ShowData;

StatusBar1.Panels.Items[1].Text:

='此次查询共有

'+”+1ntToStr(Form1.ADOQuery1.Recordcount)+”+条符合条件的记录';

end;

end;

procedureTForm1.RzComboBox1Click(Sender:

TObject);

begin

Edit6.Text:

二H・

一5

end;

procedureTForm1.SpeedButton5Click(Sender:

TObject);//添加空行begin

withADOQuery1do

begin

Append;

end;

end;

procedureTForm1.SpeedButton6Click(Sender:

TObject);//

begin

withADOQuery1do

begin

edit;

post;

end;

end;

procedureTForm1.SpeedButton7Click(Sender:

TObject);//

begin

ADOQuery1.Delete;

end;

procedureTForm1.Timer1Timer(Sender:

TObject);//

begin

StatusBar1.Panels.Items[3].Text:

=DateTimeToStr(Now);

end;

end.保存所有记录,删除记录

状态栏显示时间

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1