程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Access數據庫 >> 關於Access數據庫 >> DELPHI中操作ACCESS數據庫

DELPHI中操作ACCESS數據庫

編輯:關於Access數據庫
Delphi中操作Access數據庫(建立.mdb文件,壓縮數據庫)

  以下代碼在WIN2K,D6,MDAC2.6下測試通過,

  編譯好的程序在WIN98第二版無Access環境下運行成功.

  //聲明連接字符串

  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(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;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved