在Delphi中的Access技巧集

Delphi教程 2025-08-31

1.delphi中操作access数据库(建立.mdb文件,压缩数据库)

以下代码在win2k,d6,mdac2.6下测试通过,

编译好的程序在win98第二版无access环境下运行成功.

//在之前uses comobj,activex

//声明连接字符串

const

sconnectionstring = 'provider=microsoft.jet.oledb.4.0;data source=%s;'

+'jet oledb:database password=%s;';

//=============================================================================

// procedure: gettemppathfilename

// author : ysai

// date : 2003-01-27

// arguments: (none)

// result : string

//=============================================================================

function gettemppathfilename():string;

//取得临时文件名

var

spath,sfile&:array [0..254] of char;

begin

gettemppath(254,spath);

gettempfilename(spath,'~sm',0,sfile);

result:=sfile;

deletefile(pchar(result));

end;

//=============================================================================

// procedure: createaccessfile

// author : ysai

// date : 2003-01-27

// arguments: filename:string;password:string=''

// result : boolean

//=============================================================================

function createaccessfile(filename:string;password:string=''):boolean;

//建立access文件,如果文件存在则失败

var

stempfilename:string;

vcatalog:olevariant;

begin

stempfilename:=gettemppathfilename;

try

vcatalog:=createoleobject('adox.catalog');

vcatalog.create(format(sconnectionstring,[stempfilename,password]));

result:=copyfile(pchar(stempfilename),pchar(filename),true);

deletefile(stempfilename);

except

result:=false;

end;

end;

//=============================================================================

// procedure: compactdatabase

// author : ysai

// date : 2003-01-27

// arguments: afilename,apassword:string

// result : boolean

//=============================================================================

function compactdatabase(afilename,apassword:string):boolean;

//压缩与修复数据库,覆盖源文件

var

stempfilename:string;

vje:olevariant;

begin

stempfilename:=gettemppathfilename;

try

vje:=createoleobject('jro.jetengine');

vje.compactdatabase(format(sconnectionstring,[afilename,apassword]),

format(sconnectionstring,[stempfilename,apassword]));

result:=copyfile(pchar(stempfilename),pchar(afilename),false);

deletefile(stempfilename);

except

result:=false;

end;

end;

//=============================================================================

// procedure: changedatabasepassword

// author : ysai

// date : 2003-01-27

// arguments: afilename,aoldpassword,anewpassword:string

// result : boolean

//=============================================================================

function changedatabasepassword(afilename,aoldpassword,anewpassword:string):boolean;

//修改access数据库密码

var

stempfilename:string;

vje:olevariant;

begin

stempfilename:=gettemppathfilename;

try

vje:=createoleobject('jro.jetengine');

vje.compactdatabase(format(sconnectionstring,[afilename,aoldpassword]),

format(sconnectionstring,[stempfilename,anewpassword]));

result:=copyfile(pchar(stempfilename),pchar(afilename),false);

deletefile(stempfilename);

except

result:=false;

end;

end;

2.access中使用sql语句应注意的地方及几点技巧

以下sql语句在access xp的查询中测试通过

建表:

create table tab1 (

id counter,

name string,

age integer,

[date] datetime);

技巧:

自增字段用 counter 声明.

字段名为关键字的字段用方括号[]括起来,数字作为字段名也可行.

建立索引:

下面的语句在tab1的date列上建立可重复索引

create index idate on tab1 ([date]);

完成后access中字段date索引属性显示为 - 有(有重复).

下面的语句在tab1的name列上建立不可重复索引

create unique index iname on tab1 (name);

完成后access中字段name索引属性显示为 - 有(无重复).

下面的语句删除刚才建立的两个索引

drop index idate on tab1;

drop index iname on tab1;

access与sqlserver中的update语句对比:

sqlserver中更新多表的update语句:

update tab1

set a.name = b.name

from tab1 a,tab2 b

where a.id = b.id;

同样功能的sql语句在access中应该是

update tab1 a,tab2 b

set a.name = b.name

where a.id = b.id;

即:access中的update语句没有from子句,所有引用的表都列在update关键字后.

上例中如果tab2可以不是一个表,而是一个查询,例:

update tab1 a,(select id,name from tab2) b

set a.name = b.name

where a.id = b.id;

访问多个不同的access数据库-在sql中使用in子句:

select a.*,b.* from tab1 a,tab2 b in 'db2.mdb' where a.id=b.id;

上面的sql语句查询出当前数据库中tab1和db2.mdb(当前文件夹中)中tab2以id为关联的所有记录.

缺点-外部数据库不能带密码.

补充:看到ugvanxk在一贴中的答复,可以用

select * from [c:/aa/a.mdb;pwd=1111].table1;

access xp测试通过

在access中访问其它odbc数据源

下例在access中查询sqlserver中的数据

select * from tab1 in [odbc]

[odbc;driver=sql server;uid=sa;pwd=;server=127.0.0.1;database=demo;]

外部数据源连接属性的完整参数是:

[odbc;driver=driver;server=server;database=database;uid=user;pwd=password;]

其中的driver=driver可以在注册表中的

hkey_local_machine/software/odbc/odbcinst.ini/

中找到

异构数据库之间导数据参见 碧血剑 的

http://www.*delp*hi*bbs.com/delphibbs/dispq.asp?lid=1691966

access支持子查询

access支持外连接,但不包括完整外部联接,如支持

left join 或 right join

但不支持

full outer join 或 full join

access中的日期查询

注意:access中的日期时间分隔符是#而不是引号

select * from tab1 where [date]>#2002-1-1#;

在delphi中我这样用

sql.add(format(

'select * from tab1 where [date]>#%s#;',

[datetostr(date)]));

access中的字符串可以用双引号分隔,但sqlserver不认,所以为了迁移方便和兼容,

建议用单引号作为字符串分隔符.