基本思路是使用SQLDMO接口实现:
1.打开Delphi,Project菜单->Import Type Library...,在列表框中找到"Microsoft SQLDMO Object
Library(Version 8.0)"(你的版本可能和我的不一样),可以看到Class names列表框中显示出相应的类,
由于很多类名与vcl本身的类名冲突(比如TApplication,TTable等),需要修改类的名称,不过不改也没关系,
这里假设没改,点击按钮"Create Unit",进入漫长的等待过程。由于生成的文件比较大(6万多行的代码,
2.46M),这个过程很容易被误认为死机,稍候片刻。
2.修改Type Library文件。
经过漫长的等待,终于得到了来之不易的SQLDMO_TLB.pas,然而,当我们Compile的时候却发现这个单元
无法编译,提示大意为ID重复定义,这个简单,把第二个ID改成xID,编译通过。
3.程序代码
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,SQLDMO_TLB, //必须引入
StdCtrls, ComCtrls,comobj;
type
TForm3 = class(TForm)
Button1: TButton;
ProgressBar1: TProgressBar;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
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;
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
function TBackupSink.PercentComplete(const Message: WideString; Percent: Integer):HResult;
begin
Form3.ProgressBar1.Position:=percent;
result:=0;
end;
function TBackupSink.NextMedia(const Message: WideString):HResult;
begin
result:=-1;
end;
function TBackupSink.Complete(const Message: WideString):HResult;
begin
result:=1;
end;
procedure TForm3.Button1Click(Sender: TObject);
var
MySQLServer:SQLServer;
MyBackUp:BackUp2;
MyBackSink:TBackupSink;
FInterfaceConnection:integer;
BS:TBackupSink;
begin
BS:=TBackupSink.Create;
MySQLServer:=coSQLServer.Create;
MyBackUp:=coBackUp2.Create;
MySQLServer.Connect('192.169.1.10','sa','password');;
MyBackUp.Database:='DBA';
MyBackUp.Initialize:=true;
MyBackUp.PercentCompleteNotification:=1;
MyBackUp.Action:=0;//0完整备份,1差异备份,2文件组备份,3日志备份
MyBackUp.Files:='d:\dba.bak';
InterfaceConnect(MyBackUp, IID_BackupSink, BS, FInterfaceConnection);//关键是这里
MyBackUp.SQLBackup (MySQLServer);
InterfaceDisconnect(MyBackUp, IID_BackupSink,FInterfaceConnection);//
end;
end.
阅读(3148) | 评论(2) | 转发(0) |