Backup 對象 定義Microsoft? SQL Server? 的數(shù)據(jù)庫或日志備份操作.
備注
使用 Backup 對象, 你可以:
- 備份一個 SQL Server 數(shù)據(jù)庫或數(shù)據(jù)庫事務(wù)日志.
- 產(chǎn)生一個Transact-SQL BACKUP 定義備份的聲明.
- 監(jiān)聽一個備份操作,,并將狀態(tài)報告給用戶.
For SQL Server, a database delimits the largest backup unit. Though
many different database backup images can be maintained on any single
medium, a backup cannot span more than a single database. By default,
backup operations performed with the Backup object back up a complete database.
SQL Server can write a backup to one of four media types: disk, tape,
named pipe, or a proprietary media called a backup device. SQL Server
supports backup striping. A striped backup is one directed to more than a
single device. When striped, a backup is written across the devices in
equal chunks. Striping is supported to a single media type only. That
is, a backup can be written to two tape devices. A backup cannot be
written half to a tape device and the other half to a disk.
At a minimum, you must supply values for a backup source and a backup target when using the Backup object. The Database property specifies the backup operation source. SQL-DMO implements supported media types in the Backup object properties Files, Devices, Pipes, and Tapes. Use one media type property to specify the backup operation target.
To perform a complete database backup
- Create a new Backup object.
- Set the Database property, naming the database backed up.
- Set a media property to name the target device(s).
- Call the SQLBackup method.
In many installations, complete database backup is not a viable option. The Backup object offers access to a number of strategies that ensure data integrity by capturing a subset of the database image.
To back up a database transaction log
- Create a new Backup object
- Set the Database property, naming the database backed up.
- Set the Action property to SQLDMOBackup_Log.
- Set a media property to name the target device(s).
- Call the SQLBackup method.
To perform a differential backup
- Create a new Backup object
- Set the Database property, naming the database backed up.
- Set the Action property to SQLDMOBackup_Incremental.
- Set a media property to name the target device(s).
- Call the SQLBackup method.
To back up specific filegroups
- Create a new Backup object
- Set the Database property, naming the database backed up.
- Set the DatabaseFileGroups property, naming the filegroup(s) providing backup source data.
- Set a media property to name the target device(s).
- Call the SQLBackup method.
To back up specific files
- Create a new Backup object
- Set the Database property, naming the database backed up.
- Set the Action property to SQLDMOBackup_Files.
- Set the DatabaseFiles property, naming the file(s) providing backup source data.
- Set a media property to name the target device(s).
- Call the SQLBackup method.
Settings for any other Backup object properties are optional. Use the optional settings when conditions require extraordinary processing. For example, the MediaName and MediaDescription
properties provide, primarily, data used to ensure media availability
for tape devices and are applicable when the backup operation defined
will initialize the media. For more information about property
applicability and use, see individual property documentation.
/// <summary>
/// 備份MSSQL SERVER數(shù)據(jù)庫
/// </summary>
/// <param name="DSN">服務(wù)器名</param>
/// <param name="UID">用戶名</param>
/// <param name="PWD">密碼</param>
/// <param name="DB">數(shù)據(jù)庫名</param>
/// <param name="FilePath">備份文件名</param>
public static void MSSQL_BackupDatabase(string DSN, string UID, string PWD, string DB, string FilePath)
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(DSN, UID, PWD);
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = DB;
oBackup.Files = FilePath;
oBackup.BackupSetName = DB;
oBackup.BackupSetDescription = string.Format("{0} 備份", DB);
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
}
catch
{
throw;
}
finally
{
oSQLServer.DisConnect();
}
}
/// <summary>
/// 恢復(fù)MSSQL SERVER數(shù)據(jù)庫
/// </summary>
/// <param name="DSN">服務(wù)器名</param>
/// <param name="UID">用戶名</param>
/// <param name="PWD">密碼</param>
/// <param name="DB">數(shù)據(jù)庫名</param>
/// <param name="FilePath">備份文件名</param>
public static void MSSQL_RestoreDatabase(string DSN, string UID, string PWD, string DB, string FilePath)
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(DSN, UID, PWD);
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = DB;
oRestore.Files = FilePath;
oRestore.FileNumber = 1;
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
}
catch
{
throw;
}
finally
{
oSQLServer.DisConnect();
}
}