title: Backup from database with SMO failed
body: <p>Backup from SQL Server 2008 R2 databese using SMO is not working :</p>
  
  <blockquote>
    <p><code>BACKUP DATABASE</code> is terminating abnormally.<br>
    Source=.Net SqlClient Data Provider<br>
    ErrorCode=-2146232060<br>
    Class=16<br>
    LineNumber=1<br>
    Number=3201<br>
    Procedure=""<br>
    Server=(local)<br>
    State=1  </p>
    
    <p>StackTrace:<br>
    InnerException: Microsoft.SqlServer.Management.Common.ExecutionFailureException<br>
    Message=An exception occurred while executing a Transact-SQL statement or batch.<br>
    Source=Microsoft.SqlServer.ConnectionInfo  </p>
    
    <p>StackTrace:<br>
    at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)<br>
    at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType)<br>
    at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries)<br>
    at Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(Server server, StringCollection queries)<br>
    at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)  </p>
    
    <p>InnerException: System.Data.SqlClient.SqlException<br>
    at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException)<br>
    at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)  </p>
  </blockquote>
  
  <p>My code:</p>
  
  <pre><code>class Backup_Restore 
  {
      public string BackUpConString = @"Data Source=(local);Initial Catalog=taban;Integrated Security=True";
  public string ReStoreConString = "Data Source=(local);Initial Catalog=master;Integrated Security=True";
  
      public void BackUpMyDB()
      {
          using (SqlConnection con = new SqlConnection(BackUpConString))
          {
              ServerConnection srvConn = new ServerConnection(con);
  
              Server srvr = new Server(srvConn);
  
              if (srvr != null)
              {
                  try
                  {
                      Backup bkpDatabase = new Backup();
                      bkpDatabase.Action = BackupActionType.Database;
                      bkpDatabase.Database = "taban";
                      SaveFileDialog sfd = new SaveFileDialog();
                      sfd.Filter = "BackUp File|*.taban";
                      sfd.FileName = "BackUp_" + (DateTime.Now.ToShortDateString().Replace('/', '.'));
                      if (sfd.ShowDialog() == DialogResult.OK)
                      {
                          BackupDeviceItem bkpDevice = new BackupDeviceItem(sfd.FileName, DeviceType.File);
                          bkpDatabase.Devices.Add(bkpDevice);
                          bkpDatabase.SqlBackup(srvr);
  
                          MessageBox.Show("Bakup of Database successfully created", "Server", MessageBoxButtons.OK, MessageBoxIcon.Information);
                      }
                  }
                  catch (Exception e) { 
                      MessageBox.Show(e.ToString()); }
              }
          }
      }
         public void ReStorMyDB()
      {
          if (MessageBox.Show("All Data Stored in the Database may change!!! \n If you agree, select \"Yes\".", "DataBase ReStore", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
          {
  
              SqlConnection.ClearAllPools();
              using (SqlConnection con = new SqlConnection(BackUpConString))
              {
                  ServerConnection srvConn = new ServerConnection(con);
  
                  Server srvr = new Server(srvConn);
  
  
                  if (srvr != null)
                  {
                      try
                      {
  
                          Restore rstDatabase = new Restore();
                          rstDatabase.Action = RestoreActionType.Database;
                          rstDatabase.Database = "taban";
                          OpenFileDialog opfd = new OpenFileDialog();
                          opfd.Filter = "BackUp File|*.taban";
                          if (opfd.ShowDialog() == DialogResult.OK)
                          {
  
  
                              BackupDeviceItem bkpDevice = new BackupDeviceItem(opfd.FileName, DeviceType.File);
  
                              rstDatabase.Devices.Add(bkpDevice);
                              rstDatabase.ReplaceDatabase = true;
                              rstDatabase.SqlRestore(srvr);
                              MessageBox.Show("Database succefully restored", "Server", MessageBoxButtons.OK, MessageBoxIcon.Information);
                          }
                      }
                      catch (Exception e)
                      {
                          MessageBox.Show("ERROR: An error ocurred while restoring the database", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                      }
                  }
              }
  
          }
      }
  </code></pre>