Ajax toolkit
Ajax tutorials
12.2. Backing Up and Restoring DataThe SMO backup and restore classes serve several purposes:
The SMO backup and restore classes are described in Table 12-4. These classes are located in the Microsoft.SqlServer.Management.Smo namespace.
This section shows how to use SMO backup and restore classes programmatically. This example backs up the AdventureWorks database to a file:
using System;
using System.Data;
using System.Collections;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
class Program
{
static void Main(string[] args)
{
BackupDeviceItem bdi =
new BackupDeviceItem("AdventureWorks.bak", DeviceType.File);
Backup bu = new Backup( );
bu.Database = "AdventureWorks";
bu.Devices.Add(bdi);
bu.Initialize = true;
// add percent complete and complete event handlers
bu.PercentComplete +=
new PercentCompleteEventHandler(Backup_PercentComplete);
bu.Complete +=new ServerMessageEventHandler(Backup_Complete);
Server server = new Server("localhost");
bu.SqlBackup(server);
Console.WriteLine(Environment.NewLine + "Press any key to continue.");
Console.ReadKey( );
}
protected static void Backup_PercentComplete(
object sender, PercentCompleteEventArgs e)
{
Console.WriteLine(e.Percent + "% processed.");
}
protected static void Backup_Complete(object sender, ServerMessageEventArgs e)
{
Console.WriteLine(Environment.NewLine + e.ToString( ));
}
}
Once complete, the backup file AdventureWorks.bak is located (by default) in the C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup directory. Results are shown in Figure 12-6. Figure 12-6. Results for backup example![]() The Backup class provides programmatic access to SQL Server backup operations. Three properties are set to configure the Backup object in this example:
There are other optional properties that further control the backup operation. A PercentCompleteEventHandler is added so that the progress of the backup operation can be displayed to the console. A ServerMessageEventHandler is added to display the status of the BACKUP DATABASE operation. Finally, the SqlBackup( ) method is called to perform the database backup operation. The following example restores the backup created in the preceding example:
using System;
using System.Data;
using System.Collections;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
class Program
{
static void Main(string[] args)
{
Server server = new Server("localhost");
BackupDeviceItem bdi = new BackupDeviceItem(
"AdventureWorks.bak", DeviceType.File);
Restore r = new Restore( );
r.Database = "AdventureWorks";
r.ReplaceDatabase = true;
r.Devices.Add(bdi);
//add percent complete and complete event handlers
r.PercentComplete +=
new PercentCompleteEventHandler(Restore_PercentComplete);
r.Complete += new ServerMessageEventHandler(Restore_Complete);
r.SqlRestore(server);
Console.WriteLine(Environment.NewLine + "Press any key to continue.");
Console.ReadKey( );
}
protected static void Restore_PercentComplete(
object sender, PercentCompleteEventArgs e)
{
Console.WriteLine(e.Percent + "% processed.");
}
protected static void Restore_Complete(object sender, ServerMessageEventArgs e)
{
Console.WriteLine(Environment.NewLine + e.ToString( ));
}
}
Results are shown in Figure 12-7. The Restore class provides programmatic access to SQL Server restore operations. Three properties are set to configure the Restore object in this example: Figure 12-7. Results for restore example![]()
There are other optional properties not used in this example that further control the backup operation. A PercentCompleteEventHandler event handler is added so that the progress of the restore operation can be displayed to the console. A ServerMessageEventHandler event handler is added to display the details of the RESTORE DATABASE event. Finally, the SqlRestore( ) method is called to perform the database restore operation.
|
Ajax toolkit
Ajax tutorials