我使用Microsoft.SqlServer.Management.Smo備份數據庫。我的問題是如何驗證備份文件是否可恢復並且能正常工作。在互聯網上花了一些時間之後,我發現以下T-SQL驗證,但在Microsoft.Management.Smo程序集中有一種方法。我期待一些代碼示例。使用Microsoft.Management.Smo驗證備份文件.bak
RESTORE VERIFYONLY
FROM DISK = 'C:\Test\Test.bak'
WITH CHECKSUM
以下是我的代碼示例。
using System;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
namespace BackupApplication
{
class DatabaseBackup
{
public void DoDataBaseBackup()
{
string connectionString = ConfigurationManager.ConnectionStrings["Test"].ToString();
SqlConnection connecton = new SqlConnection(connectionString);
ServerConnection serverConnection = new ServerConnection(connecton);
Server myServer = new Server(serverConnection);
//Using windows authentication
myServer.ConnectionContext.Connect();
Database myDatabase = myServer.Databases[ConfigurationManager.AppSettings["DatabaseNameToBackup"]];
//Backup operation
Backup bkpDBFull = new Backup();
/* Specify whether you want to back up database or files or log */
bkpDBFull.Action = BackupActionType.Database;
/* Specify the name of the database to back up */
bkpDBFull.Database = myDatabase.Name;
/* You can take backup on several media type (disk or tape), here I am
* using File type and storing backup on the file system */
string destinationFolderForDatabase = ConfigurationManager.AppSettings["DestinationFolderForDatabase"];
bkpDBFull.Devices.AddDevice(destinationFolderForDatabase, DeviceType.File);
bkpDBFull.BackupSetName = "Test database Backup";
bkpDBFull.BackupSetDescription = "test database - Full Backup";
/* You can specify the expiration date for your backup data
* after that date backup data would not be relevant */
bkpDBFull.ExpirationDate = DateTime.Today.AddDays(10);
/* You can specify Initialize = false (default) to create a new
* backup set which will be appended as last backup set on the media. You
* can specify Initialize = true to make the backup as first set on the
* medium and to overwrite any other existing backup sets if the all the
* backup sets have expired and specified backup set name matches with
* the name on the medium */
bkpDBFull.Initialize = false;
/* You can specify Incremental = false (default) to perform full backup or Incremental = true to perform differential backup since most recent full backup */
bkpDBFull.Incremental = false;
/* Wiring up events for progress monitoring */
bkpDBFull.PercentComplete += CompletionStatusInPercent;
bkpDBFull.Complete += Backup_Completed;
/* SqlBackup method starts to take back up
* You can also use SqlBackupAsync method to perform the backup
* operation asynchronously */
bkpDBFull.SqlBackup(myServer);
if (myServer.ConnectionContext.IsOpen)
{
myServer.ConnectionContext.Disconnect();
}
}
private void Backup_Completed(object sender, ServerMessageEventArgs e)
{
Console.WriteLine("TestDatabase Backup Completed.");
Console.WriteLine(e.Error.Message);
}
private void CompletionStatusInPercent(object sender, PercentCompleteEventArgs e)
{
Console.Clear();
Console.WriteLine("Percent completed: {0}%.", e.Percent);
}
}
}
的Program.cs
using System;
namespace BackupApplication
{
class Program
{
static void Main(string[] args)
{
DatabaseBackup databaseBackup = new DatabaseBackup();
databaseBackup.DoDataBaseBackup();
Console.ReadLine();
}
}
}
App.Config中
<connectionStrings>
<add name="TestDatabase" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=SSPI"/>
</connectionStrings>
<appSettings>
<add key="DestinationFolderForDatabase" value="C:\Test.bak"/>
<add key="DatabaseNameToBackup" value="Test"/>
</appSettings>
你是什麼意思'.bak是否驗證? – LONG
_「我在等待一些代碼示例。」 - 嗯,這裏有一些帶有示例的文檔。 [Restore.SqlVerify](https://msdn.microsoft.com/en-us/library/ms209758.aspx)有一個_「如何創建備份並驗證它是否可讀且完整。」的樣本,儘管你可能想使用[超載,返回錯誤消息](https://msdn.microsoft.com/en-us/library/ms209837.aspx) – stuartd
一旦備份完成創建一個「恢復」,並驗證同樣的'BackupDeviceItem'用於創建備份。 – Nkosi