#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
#endregion
namespace VerifiedBackup
{
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Server s2k5 = new Server();
foreach (Database db in s2k5.Databases) // Loop through the databases
{
Databases.Items.Add(db.Name); // Add database name to the ListBox
}
}
private void DoBackup_Click(object sender, EventArgs e)
{
// Get database name from the ListBox
string db = Databases.SelectedItem.ToString();
string dbBck = «C:» + db + «.bak»; // Must escape the backslash in C#
Backup bck = new Backup(); // Instantiate a Backup object
bck.Action = BackupActionType.Database; // Set Action property
bck.BackupSetName = db + «_BackupSet»; // Set BackupSetName property
bck.Database = db; // Set Database name property
bck.DeviceType = DeviceType.File; // Set DeviceType property
// Add method adds file to Devices collection
// Must escape the backslash in C#
bck.Devices.Add(dbBck);
// Instantiate a Server object and
// invoke Backup object?s SqlBackup method
Server s2k5 = new Server();
bck.SqlBackup(s2k5);
Restore rst = new Restore(); // Instantiate a Restore object
rst.DeviceType = DeviceType.File; // Set DeviceType property
// Add method adds file to Devices collection
rst.Devices.Add(dbBck);
if (rst.SqlVerify(s2k5))
{ // Similar to VERIFY RESTOREONLY
MessageBox.Show(db + « backup verified»);
}
else
{
MessageBox.Show(db + « backup error»);
}
}
}
}