Freigeben über


Sichern und Wiederherstellen von Datenbanken und Transaktionsprotokollen

In SMO sind die Klassen Backup und Restore Dienstprogrammklassen, die die Tools zur Durchführung bestimmter Tasks, wie Sichern und Wiederherstellen, bereitstellen. Ein Backup-Objekt stellt eine spezifische Sicherungstask dar, die anstelle eines MicrosoftSQL Server-Objekts auf der Serverinstanz erforderlich ist.

Wenn Datenverlust oder -beschädigung auftritt, muss die Sicherung entweder vollständig oder teilweise wiederhergestellt werden. Die partielle Wiederherstellung verwendet die FileGroupCollection-Auflistung, um die zu wiederherstellenden Daten zu unterteilen. Bei der Sicherung eines Transaktionsprotokolls erfolgt die Datenwiederherstellung bis zu einem gewissen Zeitpunkt mithilfe der ToPointInTime-Eigenschaft des Restore-Objekts. Die Daten können auch mithilfe der SqlVerify-Methode validiert werden. Der empfohlene Sicherungsvorgang besteht aus der Prüfung der Sicherung auf Integrität durch die Vornahme eines Wiederherstellungsvorgangs und durch die regelmäßige Prüfung der Daten in der Datenbank.

Wie auch das Backup-Objekt muss das Restore-Objekt nicht mithilfe einer Create-Methode erstellt werden, da es kein Objekt auf der Instanz von SQL Server darstellt. Beim Restore-Objekt handelt es sich um eine Gruppe von Eigenschaften und Methoden, die zur Wiederherstellung einer Datenbank verwendet werden.

Beispiele

Um die bereitgestellten Codebeispiele verwenden zu können, müssen Sie die Programmierumgebung, die Programmiervorlage und die Programmiersprache wählen, in der die Anwendung erstellt werden soll. Weitere Informationen finden Sie unter Vorgehensweise: Erstellen eines Visual Basic-SMO-Projekts in Visual Studio .NET oder Vorgehensweise: Erstellen eines Visual C#-SMO-Projekts in Visual Studio .NET.

Sichern von Datenbanken und Transaktionsprotokollen in Visual Basic

In diesem Codebeispiel wird gezeigt, wie eine vorhandene Datenbank in einer Datei gesichert und wiederhergestellt wird.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Store the current recovery model in a variable.
Dim recoverymod As Integer
recoverymod = db.DatabaseOptions.RecoveryModel
'Define a Backup object variable. 
Dim bk As New Backup
'Specify the type of backup, the description, the name, and the database to be backed up.
bk.Action = BackupActionType.Database
bk.BackupSetDescription = "Full backup of Adventureworks"
bk.BackupSetName = "AdventureWorks Backup"
bk.Database = "AdventureWorks"
'Declare a BackupDeviceItem by supplying the backup device file name in the constructor, and the type of device is a file.
Dim bdi As BackupDeviceItem
bdi = New BackupDeviceItem("Test_Full_Backup1", DeviceType.File)
'Add the device to the Backup object.
bk.Devices.Add(bdi)
'Set the Incremental property to False to specify that this is a full database backup.
bk.Incremental = False
'Set the expiration date.
Dim backupdate As New Date
backupdate = New Date(2006, 10, 5)
bk.ExpirationDate = backupdate
'Specify that the log must be truncated after the backup is complete.
bk.LogTruncation = BackupTruncateLogType.Truncate
'Run SqlBackup to perform the full database backup on the instance of SQL Server.
bk.SqlBackup(srv)
'Inform the user that the backup has been completed.
Console.WriteLine("Full Backup complete.")
'Remove the backup device from the Backup object.
bk.Devices.Remove(bdi)
'Make a change to the database, in this case, add a table called test_table.
Dim t As Table
t = New Table(db, "test_table")
Dim c As Column
c = New Column(t, "col", DataType.Int)
t.Columns.Add(c)
t.Create()
'Create another file device for the differential backup and add the Backup object.
Dim bdid As BackupDeviceItem
bdid = New BackupDeviceItem("Test_Differential_Backup1", DeviceType.File)
'Add the device to the Backup object.
bk.Devices.Add(bdid)
'Set the Incremental property to True for a differential backup.
bk.Incremental = True
'Run SqlBackup to perform the incremental database backup on the instance of SQL Server.
bk.SqlBackup(srv)
'Inform the user that the differential backup is complete.
Console.WriteLine("Differential Backup complete.")
'Remove the device from the Backup object.
bk.Devices.Remove(bdid)
'Delete the AdventureWorks database before restoring it.
srv.Databases("AdventureWorks").Drop()
'Define a Restore object variable.
Dim rs As Restore
rs = New Restore
'Set the NoRecovery property to true, so the transactions are not recovered.
rs.NoRecovery = True
'Add the device that contains the full database backup to the Restore object.
rs.Devices.Add(bdi)
'Specify the database name.
rs.Database = "AdventureWorks"
'Restore the full database backup with no recovery.
rs.SqlRestore(srv)
'Inform the user that the Full Database Restore is complete.
Console.WriteLine("Full Database Restore complete.")
'Remove the device from the Restore object.
rs.Devices.Remove(bdi)
'Set te NoRecovery property to False.
rs.NoRecovery = False
'Add the device that contains the differential backup to the Restore object.
rs.Devices.Add(bdid)
'Restore the differential database backup with recovery.
rs.SqlRestore(srv)
'Inform the user that the differential database restore is complete.
Console.WriteLine("Differential Database Restore complete.")
'Remove the device.
rs.Devices.Remove(bdid)
'Set the database recovery mode back to its original value.
srv.Databases("AdventureWorks").DatabaseOptions.RecoveryModel = recoverymod
'Drop the table that was added.
srv.Databases("AdventureWorks").Tables("test_table").Drop()
srv.Databases("AdventureWorks").Alter()
'Remove the backup files from the hard disk.
My.Computer.FileSystem.DeleteFile("C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\Test_Full_Backup1")
My.Computer.FileSystem.DeleteFile("C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\Test_Differential_Backup1")

Sichern von Datenbanken und Transaktionsprotokollen in Visual C#

In diesem Codebeispiel wird gezeigt, wie eine vorhandene Datenbank in einer Datei gesichert und wiederhergestellt wird.

//Connect to the local, default instance of SQL Server. 
{ 
Server srv = default(Server); 
srv = new Server(); 
//Reference the AdventureWorks database. 
Database db = default(Database); 
db = srv.Databases("AdventureWorks"); 
//Store the current recovery model in a variable. 
int recoverymod = 0; 
recoverymod = db.DatabaseOptions.RecoveryModel; 
//Define a Backup object variable. 
Backup bk = new Backup(); 
//Specify the type of backup, the description, the name, and the database to be backed up. 
bk.Action = BackupActionType.Database; 
bk.BackupSetDescription = "Full backup of Adventureworks"; 
bk.BackupSetName = "AdventureWorks Backup"; 
bk.Database = "AdventureWorks"; 
//Declare a BackupDeviceItem by supplying the backup device file name in the constructor, and the type of device is a file. 
BackupDeviceItem bdi = default(BackupDeviceItem); 
bdi = new BackupDeviceItem("Test_Full_Backup1", DeviceType.File); 
//Add the device to the Backup object. 
bk.Devices.Add(bdi); 
//Set the Incremental property to False to specify that this is a full database backup. 
bk.Incremental = false; 
//Set the expiration date. 
System.DateTime backupdate = new System.DateTime(); 
backupdate = new System.DateTime(2006, 10, 5); 
bk.ExpirationDate = backupdate; 
//Specify that the log must be truncated after the backup is complete. 
bk.LogTruncation = BackupTruncateLogType.Truncate; 
//Run SqlBackup to perform the full database backup on the instance of SQL Server. 
bk.SqlBackup(srv); 
//Inform the user that the backup has been completed. 
Console.WriteLine("Full Backup complete."); 
//Remove the backup device from the Backup object. 
bk.Devices.Remove(bdi); 
//Make a change to the database, in this case, add a table called test_table. 
Table t = default(Table); 
t = new Table(db, "test_table"); 
Column c = default(Column); 
c = new Column(t, "col", DataType.Int); 
t.Columns.Add(c); 
t.Create(); 
//Create another file device for the differential backup and add the Backup object. 
BackupDeviceItem bdid = default(BackupDeviceItem); 
bdid = new BackupDeviceItem("Test_Differential_Backup1", DeviceType.File); 
//Add the device to the Backup object. 
bk.Devices.Add(bdid); 
//Set the Incremental property to True for a differential backup. 
bk.Incremental = true; 
//Run SqlBackup to perform the incremental database backup on the instance of SQL Server. 
bk.SqlBackup(srv); 
//Inform the user that the differential backup is complete. 
Console.WriteLine("Differential Backup complete."); 
//Remove the device from the Backup object. 
bk.Devices.Remove(bdid); 
//Delete the AdventureWorks database before restoring it. 
srv.Databases("AdventureWorks").Drop(); 
//Define a Restore object variable. 
Restore rs = default(Restore); 
rs = new Restore(); 
//Set the NoRecovery property to true, so the transactions are not recovered. 
rs.NoRecovery = true; 
//Add the device that contains the full database backup to the Restore object. 
rs.Devices.Add(bdi); 
//Specify the database name. 
rs.Database = "AdventureWorks"; 
//Restore the full database backup with no recovery. 
rs.SqlRestore(srv); 
//Inform the user that the Full Database Restore is complete. 
Console.WriteLine("Full Database Restore complete."); 
//Remove the device from the Restore object. 
rs.Devices.Remove(bdi); 
//Set te NoRecovery property to False. 
rs.NoRecovery = false; 
//Add the device that contains the differential backup to the Restore object. 
rs.Devices.Add(bdid); 
//Restore the differential database backup with recovery. 
rs.SqlRestore(srv); 
//Inform the user that the differential database restore is complete. 
Console.WriteLine("Differential Database Restore complete."); 
//Remove the device. 
rs.Devices.Remove(bdid); 
//Set the database recovery mode back to its original value. 
srv.Databases("AdventureWorks").DatabaseOptions.RecoveryModel = recoverymod; 
//Drop the table that was added. 
srv.Databases("AdventureWorks").Tables("test_table").Drop(); 
srv.Databases("AdventureWorks").Alter(); 
//Remove the backup files from the hard disk. 
My.Computer.FileSystem.DeleteFile("C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Backup\\Test_Full_Backup1"); 
My.Computer.FileSystem.DeleteFile("C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Backup\\Test_Differential_Backup1"); 
} 

Ausführen von Datenbankintegritätsprüfungen in Visual Basic

SQL Server stellt Prüffunktionen für die Datenintegrität zur Verfügung. In diesem Codebeispiel wird eine Datenbankkonsistenztyp-Prüfung für die angegebene Datenbank ausgeführt. In diesem Beispiel wird CheckTables verwendet. Allerdings können auch CheckAllocations, CheckCatalog oder CheckIdentityValues verwendet werden.

HinweisHinweis

Das StringCollection-Objekt erfordert einen Verweis auf den Namespace mittels imports System.Collections.Specialized-Anweisung.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Note, to use the StringCollection type the System.Collections.Specialized system namespace must be included in the imports statements.
Dim sc As StringCollection
'Run the CheckTables method and display the results from the returned StringCollection variable.
sc = db.CheckTables(RepairType.None)
Dim c As Integer
For c = 0 To sc.Count - 1
    Console.WriteLine(sc.Item(c))
Next

Ausführen von Datenbankintegritätsprüfungen in Visual C#

SQL Server stellt Prüffunktionen für die Datenintegrität zur Verfügung. In diesem Codebeispiel wird eine Datenbankkonsistenztyp-Prüfung für die angegebene Datenbank ausgeführt. In diesem Beispiel wird CheckTables verwendet. Allerdings können auch CheckAllocations, CheckCatalog oder CheckIdentityValues verwendet werden.

HinweisHinweis

Das StringCollection-Objekt erfordert einen Verweis auf den Namespace mittels imports System.Collections.Specialized-Anweisung.

//Connect to the local, default instance of SQL Server. 
{ 
Server srv = default(Server); 
srv = new Server(); 
//Reference the AdventureWorks database. 
Database db = default(Database); 
db = srv.Databases("AdventureWorks"); 
//Note, to use the StringCollection type the System.Collections.Specialized system namespace must be included in the imports statements. 
StringCollection sc = default(StringCollection); 
//Run the CheckTables method and display the results from the returned StringCollection variable. 
sc = db.CheckTables(RepairType.None); 
int c = 0; 
for (c = 0; c <= sc.Count - 1; c++) { 
    Console.WriteLine(sc.Item(c)); 
} 
}