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:aaa.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_MACHINESOFTWAREODBCODBCINST.INI
中找到
異構數據庫之間導數據參見 碧血劍
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不認,所以為了遷移方便和兼容, 建議用單引號作為字符串分隔符.