1.Delphi中操作Access數(shù)據(jù)庫(kù)(建立.mdb文件,壓縮數(shù)據(jù)庫(kù))
以下代碼在WIN2K,D6,MDAC2.6下測(cè)試通過(guò),
編譯好的程序在WIN98第二版無(wú)ACCESS環(huán)境下運(yùn)行成功.
//在之前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
//=============================================================================
//取得臨時(shí)文件名
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;
//壓縮與修復(fù)數(shù)據(jù)庫(kù),覆蓋源文件
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;
//壓縮數(shù)據(jù)庫(kù)并修改ACCESS數(shù)據(jù)庫(kù)密碼
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;
以下SQL語(yǔ)句在ACCESS XP的查詢(xún)中測(cè)試通過(guò)
建表:
Create Table Tab1 (
ID Counter,
Name string,
Age integer,
[Date] DateTime);
技巧:
自增字段用 Counter 聲明.
字段名為關(guān)鍵字的字段用方括號(hào)括起來(lái),數(shù)字作為字段名也可行.
建立索引:
下面的語(yǔ)句在Tab1的Date列上建立可重復(fù)索引
Create Index iDate ON Tab1 ([Date]);
完成后ACCESS中字段Date索引屬性顯示為 - 有(有重復(fù)).
下面的語(yǔ)句在Tab1的Name列上建立不可重復(fù)索引
Create Unique Index iName ON Tab1 (Name);
完成后ACCESS中字段Name索引屬性顯示為 - 有(無(wú)重復(fù)).
下面的語(yǔ)句刪除剛才建立的兩個(gè)索引
Drop Index iDate ON Tab1;
Drop Index iName ON Tab1;
ACCESS與SQLSERVER中的UPDATE語(yǔ)句對(duì)比:
SQLSERVER中更新多表的UPDATE語(yǔ)句:
UPDATE Tab1
SET a.Name = b.Name
FROM Tab1 a,Tab2 b
WHERE a.ID = b.ID;
同樣功能的SQL語(yǔ)句在ACCESS中應(yīng)該是
UPDATE Tab1 a,Tab2 b
SET a.Name = b.Name
WHERE a.ID = b.ID;
即:ACCESS中的UPDATE語(yǔ)句沒(méi)有FROM子句,所有引用的表都列在UPDATE關(guān)鍵字后.
上例中如果Tab2可以不是一個(gè)表,而是一個(gè)查詢(xún),例:
UPDATE Tab1 a,(Select ID,Name From Tab2) b
SET a.Name = b.Name
WHERE a.ID = b.ID;
訪問(wèn)多個(gè)不同的ACCESS數(shù)據(jù)庫(kù)-在SQL中使用In子句:
Select a.*,b.* From Tab1 a,Tab2 b In 'DB2.mdb'
Where a.ID=b.ID;
上面的SQL語(yǔ)句查詢(xún)出當(dāng)前數(shù)據(jù)庫(kù)中Tab1和db2.mdb(當(dāng)前文件夾中)中Tab2以ID為關(guān)聯(lián)的所有記錄.
缺點(diǎn)-外部數(shù)據(jù)庫(kù)不能帶密碼.
補(bǔ)充:看到ugvanxk在一貼中的答復(fù),可以用
Select * from
[c:\aa\a.mdb;pwd=1111].table1;
ACCESS XP測(cè)試通過(guò)
在ACCESS中訪問(wèn)其它ODBC數(shù)據(jù)源
下例在ACCESS中查詢(xún)SQLSERVER中的數(shù)據(jù)
SELECT * FROM Tab1 IN [ODBC]
[ODBC;Driver=SQL
Server;UID=sa;PWD=;Server=127.0.0.1;DataBase=Demo;]
外部數(shù)據(jù)源連接屬性的完整參數(shù)是:
[ODBC;DRIVER=driver;SERVER=server;DATABASE=database;UID=user;PWD=password;]
其中的DRIVER=driver可以在注冊(cè)表中的
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\
中找到
異構(gòu)數(shù)據(jù)庫(kù)之間導(dǎo)數(shù)據(jù)參見(jiàn) 碧血?jiǎng)?的
http://www./delphibbs/dispq.asp?lid=1691966
ACCESS支持子查詢(xún)
ACCESS支持外連接,但不包括完整外部聯(lián)接和多外連接,如支持
LEFT JOIN 或 RIGHT JOIN
但不支持
FULL OUTER JOIN 或 FULL JOIN
ACCESS中的日期查詢(xún)
注意:ACCESS中的日期時(shí)間分隔符是#而不是引號(hào)
Select * From Tab1 Where
[Date]>#2002-1-1#;
在DELPHI中我這樣用
SQL.Add(Format(
'Select * From Tab1
Where [Date]>#%s#;',
[DateToStr(Date)]));
ACCESS中的字符串可以用雙引號(hào)分隔,但SQLSERVER不認(rèn),所以為了遷移方便和兼容,
建議用單引號(hào)作為字符串分隔符.
ACCESS的約束
在JET SQL參考中關(guān)于約束的內(nèi)容不夠詳細(xì),可以參考SQL Server的聯(lián)機(jī)叢書(shū)
下面的SQL為a表的Name字段增加非空約束
ALTER TABLE a ADD CONSTRAINT a_checkname CHECK (Not [Name] is
Null)
注意:每一個(gè)約束都是一個(gè)對(duì)象,都有一個(gè)名字
為一個(gè)表設(shè)置主鍵
下面的語(yǔ)句把id列設(shè)置為主鍵
Alter Table [表] Add Primary Key (Id)
下面的語(yǔ)句把id列改為自動(dòng)編號(hào)類(lèi)型,并且設(shè)置為主鍵
Alter Table [表] Alter [id] Counter Constraint [表_p] Primary Key
修改ACCESS數(shù)據(jù)庫(kù)的密碼
//引用ComObj單元
//=============================================================================
// Procedure: ChangeAccessPassword
// Author : ysai
// Date : 2004-02-10
// Arguments: const AFileName : string; const AOldPassword :
string; const ANewPassword : string
// Result : Boolean
//=============================================================================
const AFileName : string;
const AOldPassword : string;
const ANewPassword : string
):Boolean;
const
SAlterDatabasePassword = 'ALTER DATABASE PASSWORD %s
%s';
var
acn : OleVariant;
sOld : string;
sNew : string;
begin
if AOldPassword = '' then
sOld := 'Null'
else
sOld := '[' + AOldPassword +
']';
if ANewPassword = '' then
sNew := 'Null'
else
sNew := '[' + ANewPassword +
']';
try
acn :=
CreateOleObject('ADODB.Connection');
//Delphi中的cmShareExclusive,ADO中的adModeShareExclusive
//用排它方式打開(kāi),直接用數(shù)字可以不用引用ADO單元
acn.Mode := 12;
acn.Provider := 'Microsoft.Jet.OLEDB.4.0';
acn.Properties('Jet OLEDB:Database Password') :=
AOldPassword;
acn.Open('Data Source=' + AFileName);
try
acn.Execute(Format(SAlterDatabasePassword,[sNew,sOld]));
finally
acn.Close;
end;
Result := True;
except
Result := False;
end;
end;