简体   繁体   English

C#备份:服务器XXX的备份失败

[英]C# Backup: Backup Failed for Server XXX

I have this code to Backup My database using C#. 我有此代码使用C#备份我的数据库。

But I get Backup Failed for Server ADMIN-PC error message. 但是我收到Backup Failed for Server ADMIN-PC错误消息的Backup Failed for Server ADMIN-PC

I can run Backup from SQL Server Management Studio but from here for some reason it gives me this error. 我可以从SQL Server Management Studio运行“备份”,但是由于某些原因从这里运行时,会出现此错误。

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. Alter DATABASE ROLL BACK IMMEDIATE命令完成与数据库的所有未完成的事务,并且说“从数据库移交以准备进行备份。

And you need to check roles and permissions in the connectionstring.. 并且您需要检查连接字符串中的角色和权限。

Can you show the SQL syntax to backup in SQL Server Management Studio? 您可以显示要在SQL Server Management Studio中备份的SQL语法吗? 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.. 备份工作需要深入了解Sever的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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