用一个ADOConnection1,我都搞不清这连接那,那连接这的。条件: ADOConnection1 link to "master" db ADOQuery1's Connection is ADOConnection1使用数据库函数 procedure SetCurrentDB(const DBName: string); begin with ADOQuery1 do try if Active then Close; SQL.Text := 'use ' + DBName; ExecSQL; except raise.... end; end;//通过SetCurrentDB(DBName);就可以切换Current的DBName的连接。Backup:procedure BackupDB(const DBName, FileName: string); begin SetCurrentDB('master'); with ADOQuery1 do try if Active then Close; SQL.Text := Format('backup database %s to disk=''%s''', [DBName, FileName]); ExecSQL; except raise ... end; end;Restore DB: procedure RestoreDB(const DBName, FileName: string); begin if not FileExists(FileName) then Exit; SetCurrentDB('master'); with ADOQuery1 do try if Active then Close; //这里还可以通过sp_who,检查一下是否有连接到DBName的连接者,你看有没需要,自己写吧 SQL.Text := Format('restore database %s from disk=''%s''', [DName, FileName]); ExecSQL; except raise ... end; end;
?????????????????????????????????????? 以上不管采用谁的都出现“Database In Use....”之错误 ??????????????????????????????????????
ADOConnection1 link to "master" db
ADOQuery1's Connection is ADOConnection1使用数据库函数
procedure SetCurrentDB(const DBName: string);
begin
with ADOQuery1 do
try
if Active then Close;
SQL.Text := 'use ' + DBName;
ExecSQL;
except
raise....
end;
end;//通过SetCurrentDB(DBName);就可以切换Current的DBName的连接。Backup:procedure BackupDB(const DBName, FileName: string);
begin
SetCurrentDB('master');
with ADOQuery1 do
try
if Active then Close;
SQL.Text := Format('backup database %s to disk=''%s''', [DBName, FileName]);
ExecSQL;
except
raise ...
end;
end;Restore DB:
procedure RestoreDB(const DBName, FileName: string);
begin
if not FileExists(FileName) then Exit;
SetCurrentDB('master');
with ADOQuery1 do
try
if Active then Close;
//这里还可以通过sp_who,检查一下是否有连接到DBName的连接者,你看有没需要,自己写吧
SQL.Text := Format('restore database %s from disk=''%s''', [DName, FileName]);
ExecSQL;
except
raise ...
end;
end;