写得很好的代码(100分)

  • 主题发起人 主题发起人 yyh0393
  • 开始时间 开始时间
Y

yyh0393

Unregistered / Unconfirmed
GUEST, unregistred user!
sql server的数据备份
{*******************************************************}
{ }
{ 数据库的操作及把连接字符串写进注册表 }
{ 作者:antic_ant }
{ }
{ 日期:2002-09-10 }
{ 引用了SQLDMO_TLB单元,只有安装sql server才能引入 }
{ 如果不delphi引入则须把dcu文件加入到工程中即可 }
{ }
{*******************************************************}
unit Umain;

interface

uses
Windows, Messages, SysUtils, Variants,SQLDMO_TLB,Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TFrmDataManager = class(TForm)
PnlTitle: TPanel;
ImgTitle: TImage;
LblTitle1: TLabel;
LblTitle2: TLabel;
ImgServer: TImage;
CmbxServer: TComboBox;
LblServer: TLabel;
BvlCon: TBevel;
LblConnection: TLabel;
RadioBtnWin: TRadioButton;
RadioBtnSql: TRadioButton;
LblUser: TLabel;
LblPassWord: TLabel;
EdtUser: TEdit;
EdtPassWord: TEdit;
LblDatabase: TLabel;
BvlData: TBevel;
LblDatabaseLst: TLabel;
CmbxDatabase: TComboBox;
BvlOperate: TBevel;
BtnConnection: TButton;
BtnBackUp: TButton;
BtnRestore: TButton;
BtnExit: TButton;
SaveDlg: TSaveDialog;
OpenDlg: TOpenDialog;
procedure BtnExitClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure RadioBtnWinClick(Sender: TObject);
procedure RadioBtnSqlClick(Sender: TObject);
procedure BtnConnectionClick(Sender: TObject);
procedure CmbxDatabaseDropDown(Sender: TObject);
procedure CmbxDatabaseCloseUp(Sender: TObject);
procedure BtnBackUpClick(Sender: TObject);
procedure BtnRestoreClick(Sender: TObject);
private
{ Private declarations }
procedure SetBtnState(value:Boolean);
procedure SetEdtState;
public
{ Public declarations }
Connected:boolean;
end;

var
FrmDataManager: TFrmDataManager;
App:_application;
Backup :_Backup;
Restore :_Restore;

implementation

uses reg;

{$R *.dfm}

procedure TFrmDataManager.BtnExitClick(Sender: TObject);
begin
Close;
end;

procedure TFrmDataManager.SetBtnState(value: Boolean);
begin
BtnBackUp.Enabled:=Value;
BtnRestore.Enabled:=Value;
end;

procedure TFrmDataManager.SetEdtState;
begin
EdtUser.Enabled:=False;
EdtPassWord.Enabled:=False;
EdtUser.Color:=clBtnFace;
EdtPassWord.Color:=clBtnFace;
if RadioBtnSql.Checked then
begin
EdtUser.Enabled:=True;
EdtPassWord.Enabled:=True;
EdtUser.Color:=clWhite;
EdtPassWord.Color:=clWhite;
end;
end;

procedure TFrmDataManager.FormCreate(Sender: TObject);
var
i:integer;
nl:namelist;
Server:_sqlserver;
begin
Connected:=False;
BtnConnection.Enabled:=False;
SetBtnState(Connected);
App:=coapplication.Create;
Server:=cosqlserver.Create ;
nl:=App.ListAvailableSQLServers;
CmbxServer.Clear;
for i:=0 to nl.Count-1 do
begin
CmbxServer.Items.Add(nl.Item(i+1) )
end;
CmbxServer.ItemIndex:=0;
end;

procedure TFrmDataManager.RadioBtnWinClick(Sender: TObject);
begin
SetEdtState;
end;

procedure TFrmDataManager.RadioBtnSqlClick(Sender: TObject);
begin
SetEdtState;
end;

procedure TFrmDataManager.BtnConnectionClick(Sender: TObject);
var
ConStr:string;
begin
Connected:=False;

if RadioBtnWin.Checked then
begin
ConStr:=BaseConStr+'Integrated Security=SSPI;Persist Security Info=False;'+
'Initial Catalog='+CmbxDatabase.Text +';Data Source='+
CmbxServer.Text;
end
else
begin
ConStr:=BaseConStr+'Password='+EdtPassWord.Text
+';Persist Security Info=True;User ID='+EdtUser.Text
+';Initial Catalog='+CmbxDatabase.Text
+';Data Source='+CmbxServer.Text;
end;

Connected:=True;
{$IFNDEF BizUnit}
SetRegString(HKEY_LOCAL_MACHINE, MyRegSection, MyRegKey_Connection, ConStr);
{$ENDIF}

SetBtnState(Connected);

end;

procedure TFrmDataManager.CmbxDatabaseDropDown(Sender: TObject);
var
i:integer;
Server:_sqlserver;
Dtbase:_database;
begin
BtnConnection.Enabled:=False;
Server:=cosqlserver.Create ;
Dtbase:=codatabase.Create;
CmbxDatabase.Clear;
Server.LoginTimeout:=-1;
Screen.Cursor := crHourGlass;

try
if RadioBtnWin.Checked then
begin
With Server do
begin
LoginSecure:=true;
AutoReConnect:= False;
Connect(CmbxServer.Text,null,null);
end;
end
else
begin
With Server do
begin
LoginSecure := False;
AutoReConnect := False;
Connect(CmbxServer.Text,EdtUser.Text, EdtPassWord.Text);
end;
end;
CmbxDatabase.Clear;
for i:=1 to Server.Databases.Count do
begin
CmbxDatabase.Items.Add(Server.Databases.Item(i,'').Name);
end;

except
Application.MessageBox('连接数据库失败!', PChar(Self.Caption), MB_OK or MB_ICONSTOP);
EdtUser.SetFocus;
end;
Screen.Cursor := crDefault;
end;

procedure TFrmDataManager.CmbxDatabaseCloseUp(Sender: TObject);
begin
if CmbxDatabase.ItemIndex>=0 then BtnConnection.Enabled:=True;
end;

procedure TFrmDataManager.BtnBackUpClick(Sender: TObject);
var
Server:_sqlserver;
Backup:_Backup;
begin
Server:=cosqlserver.Create ;
Server.LoginTimeout:=-1;
if SaveDlg.Execute then
begin
try
Screen.Cursor := crHourGlass;
if RadioBtnWin.Checked then
begin
With Server do
begin
LoginSecure:=true;
AutoReConnect:= False;
Connect(CmbxServer.Text,null,null);
end;
end
else
begin
With Server do
begin
LoginSecure := False;
AutoReConnect := False;
Connect(CmbxServer.Text,EdtUser.Text, EdtPassWord.Text);

end;
end;


Backup :=coBackup.Create ;
Backup.action:=SQLDMOBackup_DATABASE;
Backup.Database:=CmbxDatabase.Text;
Backup.Files := SaveDlg.FileName;

Backup.SQLBackup(Server);
Screen.Cursor := crDefault;
Application.MessageBox(PChar('数据库 '+CmbxDatabase.Text +' 备份成功'), PChar(Self.Caption), MB_OK or MB_ICONINFORMATION);
except
Screen.Cursor := crDefault;
Application.MessageBox(PChar('数据库 '+CmbxDatabase.Text+' 备份失败'), PChar(Self.Caption), MB_OK or MB_ICONSTOP);
end;

Screen.Cursor := crDefault;
end;
end;

procedure TFrmDataManager.BtnRestoreClick(Sender: TObject);
var
Server:_sqlserver;
Restore:_Restore;
begin
Server:=cosqlserver.Create ;
Server.LoginTimeout:=-1;
if OpenDlg.Execute then
begin
try
Screen.Cursor := crHourGlass;
if RadioBtnWin.Checked then
begin
With Server do
begin
LoginSecure:=true;
AutoReConnect:= False;
Connect(CmbxServer.Text,null,null);
end;
end
else
begin
With Server do
begin
LoginSecure := False;
AutoReConnect := False;
Connect(CmbxServer.Text,EdtUser.Text, EdtPassWord.Text);
end;
end;

Restore :=coRestore.create;
Restore.Replacedatabase:=true;
Restore.action:=SQLDMORESTORE_DATABASE;
Restore.Database:=CmbxDatabase.Text;
Restore.Files := OpenDlg.FileName;
Restore.SQLRestore(Server);
Screen.Cursor := crDefault;
Application.MessageBox(PChar('数据库 '+CmbxDatabase.Text +' 恢复成功'), PChar(Self.Caption), MB_OK or MB_ICONINFORMATION);
except
Screen.Cursor := crDefault;
Application.MessageBox(PChar('数据库 '+CmbxDatabase.Text+' 恢复失败'), PChar(Self.Caption), MB_OK or MB_ICONSTOP);
end;

Screen.Cursor := crDefault;
end;
end;

end.


*******************************************************
谁执行过这段代码
其中单元引用
uses里 SQLDMO_TLB单元在delphi7里好像没有
这个单元可是此段代码的关键
请高手指点
**********************************************************
 
SQLDMO_TLB.pas 是类型库吧,[:)]
 
看不懂,怎么没有注释
 
好像用了些ado原生对象
 
project -> Import Type Library ->Microsoft SQLDMO Object Library->create unit
生成类型库文件,要求必须安装SQL Server 的客户端
 
写一部分注释看的人可能更多的
 
看不太懂!
 
后退
顶部