you have to have the following references
using Microsoft.SqlServer.Management.Smo;
what's more the namespace Microsoft.sqlserver.smoextended is actualized in the get together called Microsoft.sqlserver.smoextended.dll which ought to be discovered in the catalog C:\program Files\microsoft SQL Server\100\sdk\assemblies\ in the event that you have SMO installed.
now the code is
private void btn_Start_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(saveFileDialog1.FileName))
{
MessageBox.Show("Select file first!");
}
else
{
BackupDb();
}
}
private void btnBackupDb_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(saveFileDialog1.FileName))
{
MessageBox.Show("Select file first!");
}
else
{
BackupDb();
}
}
private void BackupDb()
{
dbName = ((Database)cmbBackupDb.SelectedItem).Name;
Backup dbBackup = new Backup();
try
{
dbBackup.Action = BackupActionType.Database;
dbBackup.Database = dbName;
dbBackup.BackupSetName = string.Format("{0} backup set.", dbName);
dbBackup.BackupSetDescription = string.Format("Database: {0}. Date: {1}.", dbName, DateTime.Now.ToString("dd.MM.yyyy hh:m"));
dbBackup.MediaDescription = "Disk";
BackupDeviceItem device = new BackupDeviceItem(saveFileDialog1.FileName, DeviceType.File);
dbBackup.Devices.Add(device);
txtBackupSql.Text = dbBackup.Script(sqlServer);
progBar.Visible = true;
progBar.Value = 0;
string myHost = System.Net.Dns.GetHostName();
var ip_Address = Dns.GetHostEntry(myHost).AddressList.First(ipAddress => ipAddress.AddressFamily == AddressFamily.InterNetwork);
if(ip_Address.ToString()=="192.168.1.10".ToString())
{
dbBackup.Complete += new ServerMessageEventHandler(dbBackup_Complete);
dbBackup.PercentCompleteNotification = 5;
dbBackup.PercentComplete += new PercentCompleteEventHandler(dbBackup_PercentComplete);
dbBackup.SqlBackup(sqlServer);
progBar.Visible = false;
}
else
{
MessageBox.Show("You can only create or restore backup only from server computer!");
}
this.Close();
}
catch (Exception exc)
{
dbBackup.Abort();
MessageBox.Show(string.Format("Exception occured.\nMessage: {0}", exc.Message));
}
finally
{
sqlConn.Close();
}
}
void dbBackup_PercentComplete(object sender, PercentCompleteEventArgs e)
{
if (progBar.Value < progBar.Maximum)
{
if ((progBar.Value + e.Percent) <= 100)
{
progBar.Value += e.Percent;
}
else
progBar.Value = 100;
}
}
void dbBackup_Complete(object sender, ServerMessageEventArgs e)
{
MessageBox.Show("Backup complete");
lbl_confirm.Text = "DATABASE SUCCESSFULLY BACKED UP";
lbl_confirm.Visible = true;
lbl_confirm.ForeColor = Color.Green;
}