{$R *.dfm}
uses Registry,ComObj;
//ADO是微软提供的可以访问数据库的一种接口方式,要使用ADO必须安装MDAC(Microsoft Data Access Components)组件
procedure TForm1.Button1Click(Sender: TObject);
var Reg:TRegistry;
AccessDB:OleVariant;
//Variant与OLeVariant的区别:
//Variant在Delphi内部使用,如果涉及和外部程序、特别是与用其它语言开发的程序交换数据,最好用OleVariant
DBName:String;
begin
//先判断MDAC是否已安装及获取其版本号
Reg:=TRegistry.Create;
Reg.RootKey:=HKey_local_Machine;
if Reg.OpenKey('\SoftWare\Microsoft\DataAccess',False) then
begin
//Showmessage(Reg.ReadString('FullInstallVer')); 找到该项表明已安装MDAC(微软的解释)
MessageDlg('MDAC Installed! Version:'+Reg.ReadString('Version'),mtInformation,[mbOK],0);
end
else
begin
MessageDlg('MDAC Not Installed.',mtError,[mbOK],0);
Exit;
end;
Reg.CloseKey;
Reg.Free;
DBName:='f:\test.mdb';
if SysUtils.FileExists(DBName) then SysUtils.DeleteFile(DBName);
try
AccessDB:=ComObj.CreateOleObject('ADOX.CataLog');
AccessDB.Create('Provider=Microsoft.Jet.OleDB.4.0;Data Source='+DBName);
AccessDB:=Null;
except
MessageDlg('Create Access Failure,',mtError,[mbOK],0);
end;
end;