创建sqlserver数据库的备份并在C#.net中还原时-Error:本地主机备份失败 [英] when creating backup of sqlserver database and restoring in C#.net -Error :Backup failed for localhost

查看:79
本文介绍了创建sqlserver数据库的备份并在C#.net中还原时-Error:本地主机备份失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Localhost的备份失败:

此行出现错误:bkpDatabase.SqlBackup(srvr);

备份代码:

Backup failed for Localhost:

Error comes in this line : bkpDatabase.SqlBackup(srvr);

Back up code:

DataTable dtServers = SmoApplication.EnumAvailableSqlServers(false);
private static Server srvr;
private string DBpath = Application.StartupPath;

File.Delete(DBpath + "\\backup.bak");
            this.Cursor = Cursors.WaitCursor;
            // If the user has chosen a path where to save the backup file
            // Create a new backup operation
            Backup bkpDatabase = new Backup();
            // Set the backup type to a database backup
            bkpDatabase.Action = BackupActionType.Database;
            // Set the database that we want to perform a backup on
            bkpDatabase.Database = cmbDataBase.SelectedItem.ToString();
            // Set the backup device to a file
            BackupDeviceItem bkpDevice = new BackupDeviceItem(DBpath + "\\Backup.bak", DeviceType.File);
            // Add the backup device to the backup
            bkpDatabase.Devices.Add(bkpDevice);
            // Perform the backup

            bkpDatabase.SqlBackup(srvr);  //----------------error


数据库创建


Database Creation

private void CreateDatabase()
     {
         System.Data.SqlClient.SqlConnection tmpConn;
         string sqlCreateDBQuery;
         string ServerName = "192.168.10.10";//Server=localhost;Integrated security=SSPI;database=master
         //SqlConnection sqlCon = new SqlConnection("Data Source=Bappi; Integrated Security=True;");
        // ServerConnection connection = new ServerConnection(sqlCon);
         tmpConn = new SqlConnection();
         SqlConnection myConn = new SqlConnection("Data Source=(local); DATABASE =master;Integrated Security=True; user instance=true;");

         sqlCreateDBQuery = "CREATE DATABASE MyDatabase1 ON PRIMARY " +
             "(NAME = MyDatabase1_Data, " +
             "FILENAME = 'C:\\MyDatabase1Data.mdf', " +
             "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
             "LOG ON (NAME = MyDatabase1_Log, " +
             "FILENAME = 'C:\\MyDatabase1Log.ldf', " +
             "SIZE = 1MB, " +
             "MAXSIZE = 5MB, " +
             "FILEGROWTH = 10%)";

         SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, myConn);
         try
         {
             myConn.Open();
             MessageBox.Show(sqlCreateDBQuery);
             myCommand.ExecuteNonQuery();

             MessageBox.Show("Database has been created successfully!", "Create Database", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         catch (System.Exception ex)
         {
             MessageBox.Show(ex.ToString(), "Create Database", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         finally
         {
             tmpConn.Close();
         }
         return;
     }


恢复备份:


Restore backup:

CreateDatabase();
                   
                    this.Cursor = Cursors.WaitCursor;
                    // If the user has chosen the file from which he wants the database to be restored
                    // Create a new database restore operation
                    Restore rstDatabase = new Restore();
                    // Set the restore type to a database restore
                    rstDatabase.Action = RestoreActionType.Database;
                    // Set the database that we want to perform the restore onDBParam.DatabaseName;
                    rstDatabase.Database = "MyDatabase1";
                    // Set the backup device from which we want to restore, to a file
                    BackupDeviceItem bkpDevice = new BackupDeviceItem(DBpath + "\\WEBODLLOct26.bak", DeviceType.File);
                    // Add the backup device to the restore type
                    rstDatabase.Devices.Add(bkpDevice);
                    // If the database already exists, replace it
                    rstDatabase.ReplaceDatabase = true;
                    // Perform the restore
                    rstDatabase.SqlRestore(srvr);   // ----------------error

推荐答案

有关权限/安全性问题,请检查此线程

SQL服务器备份错误 [
Its about Permission/Security issue, check this thread

SQL server backup error[^]


这篇关于创建sqlserver数据库的备份并在C#.net中还原时-Error:本地主机备份失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆