1.delphi中操作access数据库(建立.mdb文件,压缩数据库)
以下代码在win2k,d6,mdac2.6下测试通过,
编译好的程序在win98第二版无access环境下运行成功.
//=============================================================================
// 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语句:
访问多个不同的access数据库-在sql中使用in子句:
select a.*,b.* from tab1 a,tab2 b in 'db2.mdb27% 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中的数据
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不认,所以为了迁移方便和兼容, 建议用单引号作为字符串分隔符.
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器