1.首先,要导入对象库定义:
打开Delphi,Project菜单->Import Type Library...,在列表框中找到"Microsoft SQLDMO Object
Library”,然后生成一个Unit单元文件,默认为SQLDMO_TLB,然后在备份窗体中加入引用:SQLDMO_TLB,还有ComObj。
2.在窗体中加入一个进度条,将总进度设置为100(或在代码中设置)。
3.定义接口:
type
TBackupSink=class(TInterfacedobject,backupsink)//实现接口
function PercentComplete(const Message: WideString; Percent: Integer): HResult; stdcall;
function NextMedia(const Message: WideString): HResult; stdcall;
function Complete(const Message: WideString): HResult; stdcall;
TBackupSink=class(TInterfacedobject,backupsink)//实现接口
function PercentComplete(const Message: WideString; Percent: Integer): HResult; stdcall;
function NextMedia(const Message: WideString): HResult; stdcall;
function Complete(const Message: WideString): HResult; stdcall;
4.实现
function TBackupSink.PercentComplete(const Message: WideString; Percent: Integer):HResult;
begin
frmBackupDatabase.prgCompress.PartsComplete:=percent;
result:=0;
frmBackupDatabase.Refresh;
Forms.Application.ProcessMessages;
end;
function TBackupSink.NextMedia(const Message: WideString):HResult;
begin
result:=-1;
end;
function TBackupSink.Complete(const Message: WideString):HResult;
begin
result:=1;
messageBox(Forms.Application.handle,'成功备份数据!','提示信息',MB_ICONINFORMATION);
end;
begin
frmBackupDatabase.prgCompress.PartsComplete:=percent;
result:=0;
frmBackupDatabase.Refresh;
Forms.Application.ProcessMessages;
end;
function TBackupSink.NextMedia(const Message: WideString):HResult;
begin
result:=-1;
end;
function TBackupSink.Complete(const Message: WideString):HResult;
begin
result:=1;
messageBox(Forms.Application.handle,'成功备份数据!','提示信息',MB_ICONINFORMATION);
end;
5.下面是备份按钮的操作
注意,引入的SQLDMO_TLB单元有许多类型定义和Delpgi冲突,如Application,建议在原Delphi的类型前加上限定,如Forms.Application。
procedure TfrmBackupDatabase.BackupDatabaseBySQLDMO;
var
MySQLServer:SQLServer;
MyBackUp:BackUp2;
MyBackSink:TBackupSink;
FInterfaceConnection:integer;
BS:TBackupSink;
begin
prgCompress.TotalParts:=100;
BS:=TBackupSink.Create;
MySQLServer:=coSQLServer.Create;
MyBackUp:=coBackUp2.Create;
MySQLServer.Connect(DBServer,DBUser,DBPassword);
MyBackUp.Database:=edtDatabaseName.Text;
MyBackUp.Initialize:=true;
MyBackUp.PercentCompleteNotification:=1;
MyBackUp.Action:=0;//0完整备份,1差异备份,2文件组备份,3日志备份
MyBackUp.Files:=edtFileName.Text;
InterfaceConnect(MyBackUp, IID_BackupSink, BS, FInterfaceConnection);//关键是这里
MyBackUp.SQLBackup (MySQLServer);
InterfaceDisconnect(MyBackUp, IID_BackupSink,FInterfaceConnection);//
end;
var
MySQLServer:SQLServer;
MyBackUp:BackUp2;
MyBackSink:TBackupSink;
FInterfaceConnection:integer;
BS:TBackupSink;
begin
prgCompress.TotalParts:=100;
BS:=TBackupSink.Create;
MySQLServer:=coSQLServer.Create;
MyBackUp:=coBackUp2.Create;
MySQLServer.Connect(DBServer,DBUser,DBPassword);
MyBackUp.Database:=edtDatabaseName.Text;
MyBackUp.Initialize:=true;
MyBackUp.PercentCompleteNotification:=1;
MyBackUp.Action:=0;//0完整备份,1差异备份,2文件组备份,3日志备份
MyBackUp.Files:=edtFileName.Text;
InterfaceConnect(MyBackUp, IID_BackupSink, BS, FInterfaceConnection);//关键是这里
MyBackUp.SQLBackup (MySQLServer);
InterfaceDisconnect(MyBackUp, IID_BackupSink,FInterfaceConnection);//
end;
function TBackupSink.PercentComplete(const Message: WideString; Percent: Integer):HResult;
begin
frmBackupDatabase.prgCompress.PartsComplete:=percent;
result:=0;
frmBackupDatabase.Refresh;
Forms.Application.ProcessMessages;
end;
function TBackupSink.NextMedia(const Message: WideString):HResult;
begin
result:=-1;
end;
function TBackupSink.Complete(const Message: WideString):HResult;
begin
result:=1;
messageBox(Forms.Application.handle,'成功备份数据!','提示信息',MB_ICONINFORMATION);
end;
begin
frmBackupDatabase.prgCompress.PartsComplete:=percent;
result:=0;
frmBackupDatabase.Refresh;
Forms.Application.ProcessMessages;
end;
function TBackupSink.NextMedia(const Message: WideString):HResult;
begin
result:=-1;
end;
function TBackupSink.Complete(const Message: WideString):HResult;
begin
result:=1;
messageBox(Forms.Application.handle,'成功备份数据!','提示信息',MB_ICONINFORMATION);
end;