如何实现备份数据库和恢复数据库????
解决方案 »
- TMemoryStream流出错:Out of System resource
- Pchar类型的转换
- 请问这个SQL语句该怎么改呀?
- 才鸟的问题,请大家帮忙解释,什么是stdcall它有什么作用,谢谢
- 关于在DataBase中Image字段的问题
- 获取光标的位置
- 请求推荐一本delphi网络编程的书?
- 把SQL Server中的表转换成Excel表
- 可用分太多。想。。。。。。如何从Outlook中导出邮件到自己做的邮件工具中。
- 请教连接表格
- 求助
- 我用application.messagebox(edit_memo.text,'提示信息',mb_ok+mb_iconinformation);我想让消息窗口的内容为edit_memo中的所输入的。但是
adoquery1.SQL.Add('backup database 数据库名 to Disk='''+filename+'''');恢复:
adoquery1.sql.add('restore database 数据库名 from disk='''+FileName+'''');
是用copyfile实现吗??
var
ls_sql: string;
logfile: textfile;
adoquery1: TADOQuery;
begin
//edtpath1:备份所用目录的输入框
if not DirectoryExists(edtpath1.Text) then
begin
if application.MessageBox('所输入文件夹不存在,是否创建?', '系统提示', mb_yesno) = idyes then
begin
MkDir(edtpath1.Text);
if IOResult <> 0 then
begin
MessageDlg('创建文件夹失败!', mtWarning, [mbOk], 0);
exit;
end;
end
else
Exit;
end; adoquery1 := TADOQuery.Create(self);
//Pb_ShjdConn是全局的数据库连接
adoquery1.Connection := Pb_ShjdConn;
try
ls_sql := 'BACKUP DATABASE newbase TO disk=''' + edtpath1.Text + '\newbase_' + formatdatetime('yymmddhhmm', now) + '.bak' + '''' + ' with format';
adoquery1.SQL.Clear;
adoquery1.sql.Add(ls_sql);
adoquery1.Prepared;
adoquery1.ExecSQL;
application.MessageBox('数据库备份成功完成!', '数据备份', mb_ok);
except on E: Exception do
begin
application.MessageBox('数据库备份失败!', '错误', mb_ok);
AssignFile(logfile, ExtractFilePath(Application.ExeName) + '\backuperr.log');
if FileExists(ExtractFilePath(Application.ExeName) + '\backuperr.log') then Append(logfile)
else Rewrite(logfile);
writeln(logfile, formatdatetime('yyyymmddhhmmss', now) + ':数据备份时:' + E.MESSAGE);
closefile(logfile);
end;
end;
end;
var
ls_sql: string;
logfile: textfile;
adoquery1: TADOQuery;
ado_ShjdConn: TADOConnection;
begin
//edtpath2:备份文件所在路径
if (copy(trim(edtpath2.Text), 2, 1) <> ':') and (copy(trim(edtpath2.Text), 1, 2) <> '\\') then
begin
application.MessageBox('路径输入不正确!', '系统提示', mb_ok);
edtpath2.SetFocus;
exit;
end;
if (copy(trim(edtpath2.Text), 3, 1) <> '\') and (copy(trim(edtpath2.Text), 1, 2) <> '\\') then
begin
application.MessageBox('路径输入不正确!', '系统提示', mb_ok);
edtpath2.SetFocus;
exit;
end;
try
ado_ShjdConn := TAdoConnection.Create(application);
//PbSTR_User:SQLSERVER数据库用户名,PbSTR_Pass:数据库密码 ado_ShjdConn.ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=' + PbSTR_User + ';password=' + PbSTR_Pass + ';Initial Catalog=master';
ado_ShjdConn.LoginPrompt := False;
ado_ShjdConn.ConnectionTimeout := 5;
ado_ShjdConn.Open;
except
application.MessageBox('连接master数据库失败!', '系统提示', mb_ok);
exit;
end;
//关闭建立的数据库全局连接
Pb_ShjdConn.Close;
adoquery1 := TADOQuery.Create(self);
adoquery1.Connection := ado_ShjdConn;
try
ls_sql := 'RESTORE DATABASE newbase FROM disk=''' + trim(edtpath2.Text) + '''';
adoquery1.SQL.Clear;
adoquery1.sql.Add(ls_sql);
adoquery1.Prepared;
adoquery1.ExecSQL;
application.MessageBox('数据库恢复成功完成!', '系统提示', mb_ok);
except on E: Exception do
begin
application.MessageBox('数据库恢复失败!', '错误', mb_ok);
AssignFile(logfile, ExtractFilePath(Application.ExeName) + '\backuperr.log');
if FileExists(ExtractFilePath(Application.ExeName) + '\backuperr.log') then Append(logfile)
else Rewrite(logfile);
writeln(logfile, formatdatetime('yyyymmddhhmmss', now) + ':数据恢复时:' + E.MESSAGE);
closefile(logfile);
end;
end;
end;