📄 connection.pas
字号:
unit connection;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids;
type
Tconnect=class
private
fadoconnection:tadoconnection;
fdatasource2:tdatasource;
fdatasource:tdatasource;
fdbgrid:tdbgrid;
fadotable:tadotable;
fadoquery:tadoquery;
fisnotnull:boolean;
fadoquery2:tadoquery;
public
constructor create;
destructor destroy;override;
procedure doconnection;
procedure disconnection;
procedure display;
procedure displaywide(num:integer;ado:TCustomADODataSet);
procedure comm;
procedure setcommtoborrow;
procedure commquery;
procedure displayquery;
procedure displaynoticedetail(str1,str2,str3:string);
procedure first(ado:tcustomadodataset);
procedure last(ado:tcustomadodataset);
procedure prior(ado:tcustomadodataset);
procedure next(ado:tcustomadodataset);
procedure delete;
procedure sql(str:string);
procedure displayquery2(str:string);
function noticequery2:boolean;
published
property adoconnection:tadoconnection read fadoconnection write fadoconnection;
property datasource:tdatasource read fdatasource write fdatasource;
property dbgrid:tdbgrid read fdbgrid write fdbgrid;
property adotable:tadotable read fadotable write fadotable;
property adoquery:tadoquery read fadoquery write fadoquery;
property datasource2:tdatasource read fdatasource2 write fdatasource2;
property isnotnull:boolean read fisnotnull write fisnotnull;
property adoquery2:tadoquery read fadoquery2 write fadoquery2;
end;
implementation
{ Tconnect }
procedure Tconnect.comm;
begin
doconnection;
fadotable.Connection:=fadoconnection;
fadotable.TableName:='book';
fadotable.Active:=true;
end;
procedure Tconnect.commquery;
begin
doconnection;
fadoquery.Connection:=fadoconnection;
fdatasource2.DataSet:=fadoquery;
fdbgrid.DataSource:=fdatasource2;
fadoquery.Open;
end;
constructor Tconnect.create;
begin
fadoconnection:=tadoconnection.Create(nil);
fadotable:=tadotable.Create(nil);
fdatasource:=tdatasource.Create(nil);
fdbgrid:=tdbgrid.Create(nil);
fadoquery:=tadoquery.Create(nil);
fdatasource2:=tdatasource.Create(nil);
fisnotnull:=false;
fadoquery2:=tadoquery.Create(nil);
end;
procedure Tconnect.delete;
begin
if fadotable.FieldByName('是否借出').AsBoolean then
begin
messagedlg('您不能删除这本书,现已外借,如果想删除请确认归还!!',mterror,[mbok],0);
exit;
end
else
begin
if messagedlg('您的操作将被执行,确认吗?',mtConfirmation,[mbyes,mbno],0)=mryes then
fadotable.Delete
else
exit;
end;
end;
destructor Tconnect.destroy;
begin
fadoconnection.Free;
fadoconnection:=nil;
fadotable.free;
fadotable:=nil;
fadoquery.Free;
fadoquery:=nil;
fdatasource.Free;
fdatasource:=nil;
fdbgrid.Free;
fdbgrid:=nil;
fdatasource2.Free;
fdatasource2:=nil;
fadoquery2.Free;
fadoquery2:=nil;
inherited;
end;
procedure Tconnect.disconnection;
begin
fadoconnection.Connected:=false;
end;
procedure Tconnect.display;
begin
doconnection;
fadotable.Connection:=fadoconnection;
fadotable.TableName:='book';
fdatasource.DataSet:=fadotable;
fdbgrid.DataSource:=fdatasource;
fadotable.Active:=true;
end;
procedure Tconnect.displaynoticedetail(str1, str2, str3: string);
begin
str1:=fadoquery2.Fields[0].AsString;
str2:=fadoquery2.Fields[1].AsString;
str3:=inttostr(fadoquery2.Fields[2].asinteger);
end;
procedure Tconnect.displayquery;
begin
doconnection;
fadoquery.Connection:=fadoconnection;
fdatasource2.DataSet:=fadoquery;
fadoquery.Close;
sql('select 图书编号,书名,是否借出,实际归还时间 from book');
fdbgrid.DataSource:=fdatasource2;
fadoquery.Open;
if not fadoquery.Eof then
fisnotnull:=true
else
fisnotnull:=false;
end;
procedure Tconnect.displayquery2(str:string);
begin
doconnection;
fadoquery2.Connection:=fadoconnection;
fadoquery2.Close;
fadoquery2.SQL.Clear;
fadoquery2.SQL.Add('select * from borrow where 图书编号 like :bh');
fadoquery2.Parameters.ParamByName('bh').Value:=str;
fadoquery2.Open;
end;
procedure Tconnect.displaywide(num: integer;ado:TCustomADODataSet);
var
I:integer;
begin
ado.Fields[0].DisplayWidth:=18;
for i:=1 to ado.fieldcount-1 do
ado.Fields[i].DisplayWidth:=num;
end;
procedure Tconnect.doconnection;
var
myprovider,str:string;
begin
str:=extractfilepath(application.ExeName)+'book.mdb';
if not fileexists(str) then
begin
application.MessageBox('无法找到数据库,请检查!','警告',0);
application.Terminate;
end;
myprovider:='Provider=Msdatashape.1;Data Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s';
fadoconnection.Connected:=false;
fadoconnection.ConnectionString:=format(myprovider,[str]);
fadoconnection.LoginPrompt:=false;
fadoconnection.Connected:=true;
end;
procedure Tconnect.first(ado: tcustomadodataset);
begin
ado.First;
end;
procedure Tconnect.last(ado: tcustomadodataset);
begin
ado.Last;
end;
procedure Tconnect.next(ado: tcustomadodataset);
begin
ado.Next;
end;
function Tconnect.noticequery2: boolean;
begin
doconnection;
fadoquery2.Connection:=fadoconnection;
fadoquery2.Close;
fadoquery2.SQL.Clear;
fadoquery2.SQL.Add('select 书名,借书人,归还日期 from borrow where 归还日期 <=ghrq');
fadoquery2.Parameters.ParamByName('ghrq').Value:=strtoint(formatdatetime('yyyymmdd',now));
fdatasource2.DataSet:=fadoquery2;
fdbgrid.DataSource:=fdatasource2;
fadoquery2.Open;
if not fadoquery2.Eof then
result:=true
else
result:=false;
end;
procedure Tconnect.prior(ado: tcustomadodataset);
begin
ado.Prior;
end;
procedure Tconnect.setcommtoborrow;
begin
fadotable.Connection:=fadoconnection;
fadotable.TableName:='borrow';
fadotable.Active:=true;
end;
procedure Tconnect.sql(str: string);
begin
fadoquery.SQL.Clear;
fadoquery.SQL.Add(str);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -