Delphi修改Access密码,压缩与修复,建立Access数据库文件

1 unit UAccessOperate;
  2 
  3 interface
  4 
  5 uses Windows, Sysutils, ComObj, Dialogs, ActiveX;
  6 
  7 
  8 
  9 //修改ACCESS数据库密码
 10 function ChangeDatabasePassword(AFileName,AOldPassWord,ANewPassWord:string):boolean;
 11 
 12 //压缩与修复数据库,覆盖源文件
 13 function CompactDatabase(AFileName,APassWord:string):boolean;
 14 
 15 //建立Access文件,如果文件存在则失败
 16 function CreateAccessFile(FileName:String;PassWord:string=''):boolean;
 17 
 18 implementation
 19 
 20 //声明连接字符串
 21 Const
 22 SConnectionString       = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;'
 23 
 24                                +'Jet OLEDB:Database Password=%s;';
 25 
 26 
 27 
 28 //=============================================================================
 29 
 30 // Procedure: GetTempPathFileName
 31 
 32 // Author   : ysai
 33 
 34 // Date     : 2003-01-27
 35 
 36 // Arguments: (None)
 37 
 38 // Result   : string
 39 
 40 //=============================================================================
 41 
 42 function GetTempPathFileName():string;
 43 
 44 //取得临时文件名
 45 var
 46 SPath,SFile:array [0..254] of char;
 47 begin
 48 GetTempPath(254,SPath);
 49 GetTempFileName(SPath,'~SM',0,SFile);
 50 result:=SFile;
 51 DeleteFile(PChar(result));
 52 end;
 53 
 54 
 55 
 56 //=============================================================================
 57 
 58 // Procedure: CreateAccessFile
 59 
 60 // Author   : ysai
 61 
 62 // Date     : 2003-01-27
 63 
 64 // Arguments: FileName:String;PassWord:string=''
 65 
 66 // Result   : boolean
 67 
 68 //=============================================================================
 69 //建立Access文件,如果文件存在则失败
 70 function CreateAccessFile(FileName:String;PassWord:string=''):boolean;
 71 var
 72 STempFileName:string;
 73 vCatalog:OleVariant;
 74 begin
 75 STempFileName:=GetTempPathFileName;
 76 try
 77    vCatalog:=CreateOleObject('ADOX.Catalog');
 78    vCatalog.Create(format(SConnectionString,[STempFileName,PassWord]));
 79    result:=CopyFile(PChar(STempFileName),PChar(FileName),True);
 80    DeleteFile(STempFileName);
 81 except
 82    result:=false;
 83 end;
 84 end;
 85 
 86 
 87 
 88 //=============================================================================
 89 
 90 // Procedure: CompactDatabase
 91 
 92 // Author   : ysai
 93 
 94 // Date     : 2003-01-27
 95 
 96 // Arguments: AFileName,APassWord:string
 97 
 98 // Result   : boolean
 99 
100 //=============================================================================
101 
102 function CompactDatabase(AFileName,APassWord:string):boolean;
103 //压缩与修复数据库,覆盖源文件
104 var
105 STempFileName:string;
106 vJE:OleVariant;
107 begin
108 STempFileName:=GetTempPathFileName;
109 try
110    vJE:=CreateOleObject('JRO.JetEngine');
111    vJE.CompactDatabase(format(SConnectionString,[AFileName,APassWord]),
112        format(SConnectionString,[STempFileName,APassWord]));
113    result:=CopyFile(PChar(STempFileName),PChar(AFileName),false);
114    DeleteFile(STempFileName);
115 except
116    result:=false;
117 end;
118 end;
119 
120 
121 
122 //=============================================================================
123 // Procedure: ChangeDatabasePassword
124 // Author   : ysai
125 // Date     : 2003-01-27
126 // Arguments: AFileName,AOldPassWord,ANewPassWord:string
127 // Result   : boolean
128 //=============================================================================
129 //修改ACCESS数据库密码
130 function ChangeDatabasePassword(AFileName,AOldPassWord,ANewPassWord:string):boolean;
131 var
132   STempFileName:string;
133   vJE:OleVariant;
134 begin
135 STempFileName:=GetTempPathFileName;
136 try
137    vJE:=CreateOleObject('JRO.JetEngine');
138    vJE.CompactDatabase(format(SConnectionString,[AFileName,AOldPassWord]),
139                        format(SConnectionString,[STempFileName,ANewPassWord]));
140    result:=CopyFile(PChar(STempFileName),PChar(AFileName),false);
141    DeleteFile(STempFileName);
142 except
143    result:=false;
144 end;
145 end;
146 
147 end.
posted @ 2012-12-27 10:38  马儿快跑  阅读(2366)  评论(0编辑  收藏  举报