用SQLDMO对象,下面是一段VB Script,你可以改写一下。
' Create a Backup object and set action and source database properties.
Dim oBackup As New SQLDMO.Backup
oBackup.Action = SQLDMOBackup_Files
oBackup.Database = "Northwind"oBackup.DatabaseFiles = "Northwind_txt1"' Example illustrates backup implemented to a single operating system
' file. A file naming convention could be easily applied allowing
' rapid identification of a specific backup.
oBackup.Files = "c:\program files\microsoft sql server\mssql\backup\NorthText.bak"' Optional. When backup is directed to one or more files, set media
' name, backup set name and description to provide in-file documentation
' of the file and backup set contained.
oBackup.MediaName = "NorthText.bak " & Date & " " & Time
oBackup.BackupSetName = "NorthDBFileText"
oBackup.BackupSetDescription = _
"Backup of a database file by logical name."' Call SQLBackup method to perform the backup. In a production
' environment, consider wrapping the method call with a wait pointer
' or use Backup object events to provide feedback to the user.
'
' Note: Create and connect of SQLServer object used is not
' illustrated in this example.
oBackup.SQLBackup oSQLServer
' Create a Backup object and set action and source database properties.
Dim oBackup As New SQLDMO.Backup
oBackup.Action = SQLDMOBackup_Files
oBackup.Database = "Northwind"oBackup.DatabaseFiles = "Northwind_txt1"' Example illustrates backup implemented to a single operating system
' file. A file naming convention could be easily applied allowing
' rapid identification of a specific backup.
oBackup.Files = "c:\program files\microsoft sql server\mssql\backup\NorthText.bak"' Optional. When backup is directed to one or more files, set media
' name, backup set name and description to provide in-file documentation
' of the file and backup set contained.
oBackup.MediaName = "NorthText.bak " & Date & " " & Time
oBackup.BackupSetName = "NorthDBFileText"
oBackup.BackupSetDescription = _
"Backup of a database file by logical name."' Call SQLBackup method to perform the backup. In a production
' environment, consider wrapping the method call with a wait pointer
' or use Backup object events to provide feedback to the user.
'
' Note: Create and connect of SQLServer object used is not
' illustrated in this example.
oBackup.SQLBackup oSQLServer
Query.ExecSQL;
with AdoCommand do
begin
CommandText := 'backup database dbname to disk=''' + sFileName + ''' with init';
Execute;
end;
query1.databasename:=数据库名;
tempsql:='execute sp_addumpdevice "disk","'+备份设备名+'","'+备份文件名+'"';
query1.close;
query1.sql.text:=tempsql;
try
query1.execsql;
except
application.MessageBox('数据备份失败!','提示',0);
exit;
end;
query1.close;
query1.sql.text:='backup database '+数据库名+' to '+uppercase(备份设备名);
try
query1.execsql;
application.messagebox('数据备份成功!!','提示',0);
except
application.messagebox('数据备份失败!!','提示',0);
end;
end;
procedure Tfrmmain.RestoreData;
begin
with Query do
begin
Close; SQL.Clear;
if(Length(Label3.Caption)>3)then
SQL.Text:=Format('restore database rsdata from disk=''%s'' with replace',
[Label3.Caption+'\rsdata.bak'])
else
SQL.Text:=Format('restore database rsdata from disk=''%s'' with replace',
[Label3.Caption+'rsdata.bak']);
Prepare;
try
Caption:='正在恢复数据,请稍候...';
DataBase1.HandleShared := False;
ExecSQL;
Caption:='数据备份/恢复【数据恢复】';
MessageBox(Self.Handle,'数据恢复成功,即将退出!','确定',
MB_OK+MB_ICONINFORMATION+MB_APPLMODAL);
Close;
except
MessageBox(Self.Handle,'请确定已经断开数据库的所有连接后再恢复!','恢复出错',
MB_OK+MB_ICONERROR+MB_APPLMODAL);
Exit;
end;
end;
end;//备份数据过程
procedure Tfrmmain.BackupData;
var
Ssql: string;
begin
Ssql:='backup database rsdata to disk=:file with init';
with Query do
begin
Close; SQL.Clear;
SQL.Add(Ssql);
if(Length(frmmain.Label3.Caption)>3)then
Params[0].Value:=frmmain.Label3.Caption+'\rsdata.bak'
else
Params[0].Value:=frmmain.Label3.Caption+'rsdata.bak';
prepare;
try
Caption:='正在备份数据,请稍候...';
Screen.Cursor := crHourGlass;
ExecSQL;
Caption:='数据备份/恢复【数据备份】';
Close;
except
MessageBox(Self.Handle,'数据备份出错!,请重新开始','错误',
MB_OK+MB_ICONERROR+MB_APPLMODAL);
Exit;
end;
end;
end;
执行数据恢复操作时要断开所有与数据库的联接