我用ADO+SQL SERVER 2000 CommonData.acnConnection.DefaultDatabase := 'Master';
CommonData.acnConnection.Close;
CommonData.acnConnection.Open;
ADOQuery.Connection := nil;
ADOQuery.Connection := CommonData.acnConnection; ADOQuery.Close;
ADOQuery.SQL.Text := ' RESTORE DATABASE ' + trim(edtNewDBName.Text)
+ ' FROM DISK =' + Quotedstr(Trim(edtDBName.text)) + ' WITH replace, '
+ ' MOVE ' + Quotedstr(DataName) + ' TO '
+ Quotedstr(edtDestDir.Text + '\' + Trim(edtNewDBName.Text) + '.mdf ')
+ ' , MOVE ' + Quotedstr(LogName) + ' TO '
+ Quotedstr(edtDestDir.Text + '\' + Trim(edtNewDBName.Text) + '_log.ldf');}
Showmessage(ADOQuery.SQL.Text);
ADOQuery.ExecSQL;
Panel_ShowTopic.Visible := False;
Application.MessageBox(' 数据库恢复成功,请重新连接数据库,重新进入系统!', PChar(Caption), MB_ICONINFORMATION);
Application.Terminate;
CommonData.acnConnection.Close;
CommonData.acnConnection.Open;
ADOQuery.Connection := nil;
ADOQuery.Connection := CommonData.acnConnection; ADOQuery.Close;
ADOQuery.SQL.Text := ' RESTORE DATABASE ' + trim(edtNewDBName.Text)
+ ' FROM DISK =' + Quotedstr(Trim(edtDBName.text)) + ' WITH replace, '
+ ' MOVE ' + Quotedstr(DataName) + ' TO '
+ Quotedstr(edtDestDir.Text + '\' + Trim(edtNewDBName.Text) + '.mdf ')
+ ' , MOVE ' + Quotedstr(LogName) + ' TO '
+ Quotedstr(edtDestDir.Text + '\' + Trim(edtNewDBName.Text) + '_log.ldf');}
Showmessage(ADOQuery.SQL.Text);
ADOQuery.ExecSQL;
Panel_ShowTopic.Visible := False;
Application.MessageBox(' 数据库恢复成功,请重新连接数据库,重新进入系统!', PChar(Caption), MB_ICONINFORMATION);
Application.Terminate;
BackUp DataBase 通常用存储过程好些。
CommonData.acnConnection.DefaultDatabase := 'Master';
CommonData.acnConnection.Close;
CommonData.acnConnection.Open;
ADOQuery.Connection := nil;
ADOQuery.Connection := CommonData.acnConnection;
是这几句话出错。
ADOQUERY1.Close;
ADOQUERY1.SQL.Clear;
ADOQUERY1.SQL.ADD('use master declare @spid int');
ADOQUERY1.SQL.ADD('declare getspid cursor for');
ADOQUERY1.SQL.ADD('select spid from sysprocesses where dbid=db_id('+#39+'gwgl'+#39+')');
ADOQUERY1.SQL.ADD('open getspid');
ADOQUERY1.SQL.ADD('fetch next from getspid into @spid');
ADOQUERY1.SQL.ADD('while @@fetch_status < >-1');
ADOQUERY1.SQL.ADD('begin');
ADOQUERY1.SQL.ADD('exec('+#39+'kill '+#39+'+@spid)');
ADOQUERY1.SQL.ADD('fetch next from getspid into @spid');
ADOQUERY1.SQL.ADD('end');
ADOQUERY1.SQL.ADD('close getspid');
ADOQUERY1.SQL.ADD('deallocate getspid');
ADOQUERY1.SQL.ADD('RESTORE DATABASE GWGL FROM disk='+#39+OPENDIALOG1.FileName+#39+' WITH REPLACE');
TRY
ADOQUERY1.ExecSQL;
APPLICATION.MessageBox('数据恢复操作成功,请确认重新登录服务器!','林康软件',MB_OK+MB_ICONWARNING);
EXCEPT
F_WAIT.CLOSE;
APPLICATION.MessageBox('数据恢复操作失败,请确认重新登录服务器!','林康软件',MB_OK+MB_ICONWARNING);
END;
MySQL := 'ALTER DATABASE DB_CLE SET OFFLINE WITH ROLLBACK IMMEDIATE';
with ADOQueryRestor do
begin
Close;
SQL.Clear;
SQL.Text := MySQL;
try
ExecSQL;
except end;
end;
RestorStr := 'Restore DataBase DFPSS from disk=' + #39 + FINSTORE + #39 + ' with replace, '+ ' move ' + #39 + 'dfpss_data' + #39 + ' to ' + #39 + restore_file_path + 'dfpss_Data.MDF' + #39+ ', move ' + #39 + 'dfpss_log' + #39 + ' to ' + #39 + restore_file_path + 'dfpss_log.ldf' + #39;;
with ADOQueryRestor do
begin
Close;
SQL.Clear;
SQL.Text := RestorStr;
try
ExecSQL;
Application.MessageBox('恢复数据库成功!','提示!',mb_ok+mb_iconinformation);
except
Application.MessageBox('恢复数据库出错!','错误!',mb_ok+mb_iconError);
end;
end;
MySQL := 'ALTER DATABASE DB_CLE SET ONLINE WITH ROLLBACK IMMEDIATE';
with ADOQueryRestor do
begin
Close;
SQL.Clear;
SQL.Text := MySQL;
try
ExecSQL;
except end;
end;
DM.CLE_Connect.Connected := True;