简体   繁体   中英

C# Backup: Backup Failed for Server XXX

I have this code to Backup My database using C#.

But I get Backup Failed for Server ADMIN-PC error message.

I can run Backup from SQL Server Management Studio but from here for some reason it gives me this error.

How can I make this work?

private void btnBackup_Click(object sender, EventArgs e)
    {
        progressBar1.Value = 0;

        try
        {
            Server dbServer = new Server(new ServerConnection(txtServer.Text, txtUserName.Text, txtPassword.Text));
            Backup dbBackup = new Backup() {Action = BackupActionType.Database, Database = txtDatabase.Text};
            dbBackup.Devices.AddDevice(@"C:\PDM.bak", DeviceType.File);
            dbBackup.Initialize = true;
            dbBackup.PercentComplete += DbBackup_PercentComplete;
            dbBackup.PercentComplete += DbBackup_Complete;
            dbBackup.SqlBackupAsync(dbServer);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Message ", MessageBoxButtons.OK, MessageBoxIcon.Error);


        }

    }
    private void DbBackup_Complete(object sender, ServerMessageEventArgs e)
    {
        if (e.Error != null)
        {
            label5.Invoke((MethodInvoker)delegate
            {
                lblStatus.Text = e.Error.Message;
            });
        }

    }
    private void DbBackup_PercentComplete(object sender, PercentCompleteEventArgs e)
    {
        progressBar1.Invoke((MethodInvoker)delegate
        {
            progressBar1.Value = e.Percent;
            progressBar1.Update();


        });
        label5.Text = $"{e.Percent}%";
    }
}

Is that the all exception message? Try only the backup work without progressbar and see what exception is thrown. Without full picture, people here cannot help you enough.

Do you only have to use that way for backup ?

This is my code to backup database for your reference.

using (SqlConnection masterdbConn = new SqlConnection())
{
     masterdbConn.ConnectionString = localmasterConnectionString;
     masterdbConn.Open();

      using (SqlCommand rollback_dbcomm = new SqlCommand())
      {
           rollback_dbcomm.Connection = masterdbConn;
           rollback_dbcomm.CommandText = "ALTER DATABASE mydbname SET MULTI_USER WITH ROLLBACK IMMEDIATE";

           rollback_dbcomm.ExecuteNonQuery();
      }
      masterdbConn.Close();
}

SqlConnection.ClearAllPools();

using (SqlConnection backupConn = new SqlConnection())
{
     backupConn.ConnectionString = localConnectionString;
     backupConn.Open();

     using (SqlCommand backupcomm = backupConn.CreateCommand())
     {
          backupcomm.CommandText = @"BACKUP DATABASE mydbname TO DISK='c:\mydbname.bak'";

          backupcomm.ExecuteNonQuery();
     }
     backupConn.Close();
 }

Alter DATABASE ROLL BACK IMMEDIATE command finishes all the unfinished transactions with the database and let's say 'hands off from the database to be ready for Backup.

And you need to check roles and permissions in the connectionstring..

Can you show the SQL syntax to backup in SQL Server Management Studio? and the connectionstring without the credentials(like password)? And can you describe you problem in more detail?

Backup work needs deep understanding how Sever works..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM