注册 登录
编程论坛 Delphi论坛

如何备份sql server数据库

wsx87 发布于 2007-06-04 16:38, 1716 次点击
请大家帮个忙,如何用delphi备份sql server数据库?
8 回复
#2
xu20002007-06-04 22:46
执行备份语句就可以了。
#3
songyang2012007-06-05 11:35

对的,执行sql语句自身的备份语句,可以看T-sql帮助。

#4
wsx872007-06-05 18:32

可不会用啦,能不能帮我写一段啊

#5
无情杀手2007-06-05 19:50
写一段???
#6
zengziji2007-06-08 13:43
Pinformation是我的数据库名,你看着改就行了
//------------备份数据库
var
savedata:string;
findqu:tadoquery;
begin
findqu:=tadoquery.Create(self);
findqu.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;'
+'User ID=sa;Initial Catalog=master;Data Source=.';
//------------查找sysdatabases数据库,是否存在Pinformation
findqu.Close;
findqu.SQL.Clear;
findqu.SQL.Text:='select name from sysdatabases where name='
+quotedstr('Pinformation');
findqu.Open;
if trim(findqu.FieldByName('name').AsString)<>'Pinformation' then
begin
messagebox(0,'数据库不存在,不能进备份操作!','提示',MB_ICONEXCLAMATION+mb_ok);
end
else
begin
savedatalog.Filter:='ALL files (*.*)|*.*';
savedatalog.FileName:='Backup'+formatdatetime('yyyy-MM-dd hhmmss',now());
if savedatalog.Execute then
begin
savedata:=savedatalog.FileName;
Datam_backup.adoquback.Close;
Datam_backup.adoquback.SQL.Clear;
Datam_backup.adoquback.SQL.Text:='backup database Pinformation to disk= '+quotedstr(savedata);
Datam_backup.adoquback.ExecSQL;
application.MessageBox(pchar('数据库已经备份到'
+savedata+'!'),'提示',MB_OK+MB_ICONINFORMATION);
end;
end;
end;
#7
wsx872007-06-12 19:10
哦,谢谢!!1
#8
xiaxia4212007-06-13 13:16

//数据备份
var
Str_Path:string;
begin
SaveDialog1.Filter := '所有文件(*.*)|*.*';
SaveDialog1.Title := '选择备份路径和文件名';
if SaveDialog1.Execute=true then
begin
Str_Path:=SaveDialog1.FileName;
end;
if Str_Path<>'' then
begin
with DM.QSJBF do
begin
try
Screen.Cursor := crHourGlass;
DM.QSJBF.Close;
DM.QSJBF.SQL.Clear;
DM.QSJBF.SQL.Add(' BACKUP DATABASE 物流管理系统 TO DISK ='+'''' +Str_Path+'''');
DM.QSJBF.ExecSQL();
Screen.Cursor := crDefault;
Application.MessageBox('数据库备份成功完成','数据库备份',0+mb_iconinformation);
except
Screen.Cursor := crDefault;
Application.MessageBox('数据库备份失败!请检查备份路径或网络状态','数据库备份',0+mb_iconinformation);
exit;
end;
end;
end;


//数据恢复

var
Str_Path:string;
begin
DM.ADOConn.Close;
DM.ADOConn.Connected:=False;
Opendialog1.Filter := '所有文件(*.*)|*.*';
Opendialog1.Title := '选择恢复文件路径和文件名';
if Opendialog1.Execute then
Str_Path:=opendialog1.FileName;
if Str_Path<>'' then
begin
with DM.QSJHF do
begin
try
try
Screen.Cursor := crHourGlass;
Close;
SQL.Clear;
SQL.Add('use master restore DATABASE 物流管理系统 from DISK ='+'''' +Str_Path+'''');
ExecSQL();
Screen.Cursor := crDefault;
Application.MessageBox('数据库恢复成功完成!','数据库恢复',0+mb_iconinformation);
except
Screen.Cursor := crDefault;
Application.MessageBox('数据库恢复失败!请检查备份路径或网络状态','数据库恢复',0+mb_iconinformation);
exit;
end;
Finally
with DM.ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('USE 物流管理系统');
ExecSQL;
end;
end;
end;
end;
DM.ADOConn.Open;
DM.ADOConn.Connected:=True;

#9
gg5252007-07-05 16:54

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ComCtrls, ExtCtrls, DB, ADODB;

type
TForm1 = class(TForm)
Panel1: TPanel;
Label1: TLabel;
PB: TProgressBar;
StatusBar1: TStatusBar;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
dir:string;
id:DWORD;
pd:THandle;

implementation

{$R *.dfm}

procedure TForm1.BitBtn3Click(Sender: TObject);
begin
Close;
end;

function pro:Boolean;
begin
if Form1.PB.position<100 then
Form1.PB.Position:=Form1.PB.Position+1;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
try
if SaveDialog1.Execute then
begin
dir:=SaveDialog1.FileName+'.bak';
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Text:='backup database db_ems to disk='''+dir+'''';
ExecSQL;
end;
pd:=createthread(nil,0,@pro,nil,0,id);
PB.Position:=PB.Max;
ShowMessage('备份OK!');
PB.Position:=PB.Min;
end;
except
begin
ShowMessage('备份失败!');
PB.Position:=PB.Min;
end;
end;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
try
if OpenDialog1.Execute then
begin
dir:=OpenDialog1.FileName;
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Text:='use master restore database db_EMS from disk='''+dir+'''';
ExecSQL;
end;
pd:=CreateThread(nil,0,@pro,nil,0,id);
PB.Position:=PB.Max;
ShowMessage('还原OK!');
PB.Position:=PB.Min;
end;
except
showmessage('还原失败!');
pb.Position:=PB.Min;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
postmessage(Pb.Handle,$0409,0,clLime);
end;

end.
这样还简单点,呵呵

1