最好能提供源码参考!
解决方案 »
- 怎样用Application对象来创建、添加、删除主窗口的菜单?
- DBedit 中粘贴非数字报错 高分求解
- 请问作飞屏和选择access和SQL sever 有关吗?
- 急寻:access数据库备份,恢复的解决方法!谢谢!
- 关于打开窗体的问题
- 怎么样对Tchar控件进行预览显示
- chart有这样的功能:当鼠标在chart上化出一定的范围,松开鼠标后这个范围就自动放大了,请问怎么用代码实现这个功能?(当然确定范围的横
- 小弟菜求大虾帮个忙
- ADO问题!!!!
- 在图形界面里怎样运行DOS的命令
- 为何用QuickRep3.6设计的报表在有些喷墨打印机上能正常预览但打印不出来?
- 那位大侠知道TFlat控件在那里下!
用压缩/恢复数据库方法来实现Access数据库备份:unit unAccessTools;interfaceuses Sysutils,ComObj,Dialogs;function DaoActive(var DaoObject:OleVariant):Boolean;
function DaoCompactDB(const FileName:string):Boolean;
function DaoRepairDB(const FileName:string):Boolean;implementationfunction DaoActive(var DaoObject:OleVariant):Boolean;
begin
Result:=False;
try
DaoObject:=GetActiveOleObject('DAO.DBEngine.36');
Result:=True;
except
try
DaoObject:=CreateOleObject('DAO.DBEngine.36');
Result:=True;
except
DaoObject:=Null;
end;
end;
end;//压缩Access数据库
function DaoCompactDB(const FileName:string):Boolean;
var
db:OleVariant;
TempFile:string;
begin
Result:=False;
try
if not DaoActive(db) then
Exit;
try
TempFile:=ExtractFilePath(FileName)+'msaTemp.mdb';
db.CompactDatabase(FileName,TempFile);
DeleteFile(FileName);
RenameFile(TempFile,FileName);
Result:=True;
except
on E:EOleException do
ShowMessage(E.Message);
end
finally
db:=Unassigned;
end;
end;//修复Access数据库
function DaoRepairDB(const FileName:string):Boolean;
var
db:OleVariant;
begin
Result:=False;
try
if not DaoActive(db) then
Exit;
try
db.RepairDatabase(FileName);
Result:=True;
except
on E:EOleException do
ShowMessage(E.Message);
end
finally
db:=Unassigned;
end;
end;end.
沉沦中..........
我用了 COPYFILE这个函数,可是没有拷过去呀!
// 功能 : 压缩数据
// 参数 :
// CompressionLevel: 压缩比
// SourceStream: 数据源
// TargetStream: 目标数据源
// 返回值 : 是否成功
// 日期 : 2002/7/12
////////////////////////////////////////////////////////////////////////////////
function CompressStream(const CompressionLevel: TCompressionLevel;SourceStream:TMemoryStream;var TargetStream:TMemoryStream):boolean;
var
ComStream:TCompressionStream;
Count:integer;
begin
ComStream:=TCompressionStream.Create(CompressionLevel,TargetStream);
try
Count := SourceStream.Size; SourceStream.SaveToStream(ComStream);
ComStream.Free;
SourceStream.Clear;
//写入原始图像的尺寸
SourceStream.WriteBuffer(Count, SizeOf(Count));
//写入经过压缩的图像流
SourceStream.CopyFrom(TargetStream,0); TargetStream.Clear;
SourceStream.SaveToStream(TargetStream); result:=true;
except
result:=false;
end;
end;////////////////////////////////////////////////////////////////////////////////
// 功能 : 解压缩数据
// 参数 :
// SourceStream: 数据源
// TargetStream: 目标数据源
// 返回值 : 是否成功
// 日期 : 2002/7/12
////////////////////////////////////////////////////////////////////////////////
function DeCompressStream(SourceStream:TMemoryStream;var TargetStream:TMemoryStream):boolean;
var
ComStream:TDecompressionStream;
TempStream:TMemoryStream;
FStream:TMemoryStream;
Count:integer;
Buffer: PChar;
B:PChar;
begin
FStream:=TMemoryStream.Create;
TempStream:=TMemoryStream.Create;
try
try
TempStream.LoadFromStream(SourceStream);
// 将长度去掉
TempStream.ReadBuffer(Count, SizeOf(Count)); GetMem(B,TempStream.Size-SizeOf(Count)); TempStream.ReadBuffer(B^,TempStream.Size-SizeOf(Count));
TargetStream.WriteBuffer(B^,TempStream.Size-SizeOf(Count));
FreeMem(B); TempStream.Clear;
TempStream.CopyFrom(TargetStream,0); TargetStream.Clear; FStream.LoadFromStream(TempStream); ComStream:=TDecompressionStream.Create(FStream); GetMem(Buffer, Count);
ComStream.ReadBuffer(Buffer^,Count);
TargetStream.WriteBuffer(Buffer^, Count);
TargetStream.Position := 0;//复位流指针 FreeMem(Buffer); result:=true;
except
result:=false;
end;
finally
FStream.Free;
TempStream.Free;
end;
end;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ADODB, Access2000;type
TdmDBConnection = class(TDataModule)
adocAccess: TADOConnection;
adsGetSysParam: TADODataSet;
adsCAInfo: TADODataSet;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
{ Private declarations } public
{ Public declarations } AccessMaster: TAccessApplication; sDataPath: String; procedure GetSysParam(const pSection_ID, pIdent: string; var sParam: string); procedure SetSysParam(const pSection_ID, pIdent: string; sParam: string); procedure ConnectDB(MDBName: String); function PackDB(DBFileName: String): String; function CompactMDB(sSourceDB: String): String; end;var
dmDBConnection: TdmDBConnection;implementation
uses Registry, FileCtrl, JRO_TLB, uSysCommon, uSysSecCom;{$R *.DFM}procedure TdmDBConnection.GetSysParam(const pSection_ID, pIdent: string; var sParam: string);
begin
if not adocAccess.Connected then ConnectDB('SysReg.mdb');
with adsGetSysParam do
begin
if Active then
Close;
Parameters.ParamByName('Section_ID').Value := pSection_ID;
Parameters.ParamByName('Ident').Value := pIdent;
Open;
if not EOF then
sParam := FieldByName('Params').AsString
else
sParam := EmptyStr;
// ShowMessage(sParam);
Close;
end;
end;function TdmDBConnection.PackDB(DBFileName: String): String;
const
sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
var
IJetEng: JetEngine;
sOldMDB, sNewMDB, sTempFile, sErr: String;
begin
sErr := '';
sTempFile := ExtractFileDir(Application.ExeName) + '\TmpAccessDB01.mdb'; if FileExists(sTempFile) then DeleteFile(sTempFile); sOldMDB := sProvider + 'Data Source=' + DBFileName;
sNewMDB := sProvider + 'Data Source=' + sTempFile; try
try
IJetEng := CoJetEngine.Create;
IJetEng.CompactDatabase(sOldMDB, sNewMDB);
// if FileExists(DBName) then DeleteFile(DBName);
if not CopyFile(PChar(sTempFile), PChar(DBFileName), False) then
sErr := 'Fail to overwrite the old database.';
// IJetEng := Nil;
// Result := True;
except
on E: Exception do
sErr := E.Message;
end;
finally
IJetEng := Nil;
if FileExists(sTempFile) then DeleteFile(sTempFile);
if sErr <> '' then
begin
Result := 'Fail to compact database "' +DBFileName+ '".' +
'Reason: ' + sErr;
// MessageDlg(String(Result), mtError, [mbOK], 0);
end
else Result := 'Compact database "' +DBFileName+ '" successfully.';
// Result := False;
end;
end;procedure TdmDBConnection.DataModuleCreate(Sender: TObject);
const
RegPath = '\Software\Tradelink\iTTS';
var
Reg : TRegistry;
sServerPath: String;
sTemp: String;
begin AccessMaster := TAccessApplication.Create(nil); Reg := TRegistry.Create(KEY_READ);
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey(RegPath, False) then
begin
sServerPath := Reg.ReadString('ServerPath');
// LocalPath := Reg.ReadString('LocalPath');
// Local_Version := Reg.ReadString('Version');
end
Else raise Exception.Create('Unable to open registry.');
if (sServerPath = EmptyStr) or (Not DirectoryExists(sServerPath)) then
raise Exception.Create('Unable to locate the server directory: ''' + sServerPath + '''');
// if (LocalPath = EmptyStr) or (Not DirectoryExists(LocalPath)) then
// raise Exception.Create('Unable to locate the local directory: ''' + LocalPath + '''');
if sServerPath[Length(sServerPath)] <> '\' then sServerPath := sServerPath + '\';
uSysCommon.ServerPath := sServerPath;
// if LocalPath[Length(LocalPath)] <> '\' then LocalPath := LocalPath + '\';
finally
Reg.CloseKey;
Reg.Free;
end; BasePath := ExtractFilePath(Copy(sServerPath, 1, Length(sServerPath)-1));
if Length(BasePath) > 0 then
if BasePath[Length(BasePath)] <> '\' then BasePath := BasePath + '\'; sDataPath := BasePath + 'DATA'; sTemp := '';
GetSysParam('POLICY', 'LOUTDB', sTemp);
if sTemp <> '' then uSysSecCom.LOUTDB := sTemp
else uSysSecCom.LOUTDB := BasePath + 'Sec\Out'; GetSysParam('POLICY', 'LINDB', sTemp);
if sTemp <> '' then uSysSecCom.LINDB := sTemp
else uSysSecCom.LINDB := BasePath + 'Sec\In'; ConnectDB('SysReg.mdb');
end;procedure TdmDBConnection.ConnectDB(MDBName: String);
const
sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
begin
with adocAccess do
begin
Connected:= False;
ConnectionString := sProvider + 'Data Source=' + sDatapath +'\'+ MDBName;
Connected:= True;
end;
end;procedure TdmDBConnection.DataModuleDestroy(Sender: TObject);
begin
adsGetSysParam.Active := False;
adsCAInfo.Active := False;
adocAccess.Connected := False;
AccessMaster.Free;
end;procedure TdmDBConnection.SetSysParam(const pSection_ID, pIdent: string;
sParam: string);
begin
if not adocAccess.Connected then ConnectDB('SysReg.mdb');
with adsGetSysParam do
begin
if Active then
Close;
Parameters.ParamByName('Section_ID').Value := pSection_ID;
Parameters.ParamByName('Ident').Value := pIdent;
Open;
if RecordCount > 0 then
begin
First;
Edit;
FieldByName('Params').AsString := sParam;
Post;
end;
// else
// sParam := EmptyStr;
// ShowMessage(sParam);
Close;
end;
end;function TdmDBConnection.CompactMDB(sSourceDB: String): String;
var
sErr, sTmpFile: String;begin
sTmpFile := ExtractFileDir(Application.ExeName) + '\TmpAccessDB01.mdb'; if FileExists(sTmpFile) then DeleteFile(sTmpFile);
Application.ProcessMessages;
Result := 'Cannot compact the database. Reason: ';
sErr := ''; // AccessMaster := TAccessApplication.Create(nil);
try
try
AccessMaster.DBEngine.CompactDatabase(WideString(sSourceDB), WideString(sTmpFile),
EmptyParam, EmptyParam, EmptyParam);
Application.ProcessMessages;
if not CopyFile(PChar(sTmpFile), PChar(sSourceDB), False) then
begin
sErr := 'Fail to overwrite the old database.';
Abort;
end
else Result := 'Compact database "' +sSourceDB+ '" successfully.';
Application.ProcessMessages;
finally
// AccessMaster.Free;
end;
except
on E: Exception do
begin
// if AccessMaster <> nil then AccessMaster.Free;
if sErr <> '' then
Result := Result + sErr
else
Result := Result + E.Message;
end;
end;end;end.
to amei2000go(浪子):谢谢你!这两个函数直接害现了备份与恢复的功能了吗?
to Wally_wu(韦利):谢谢你!我调试时出现了很多问题,我现在头都晕了。
qwertyasd(昊) :谢谢你!