2

我正在執行從/向Microsoft SQL Server恢復/備份數據庫的應用程序。以編程方式恢復多個數據庫

如何獲取捕獲源文件夾中所有.bak文件的效果,而目標數據庫的文本框用作新數據庫的新名稱並將其還原到sql服務器中?

我的驗證是,如果目標數據庫groupbox中的名稱,它會提示錯誤而不是恢復它。

這是接口 1

這裏是我的代碼

CheckDBExist

public List<string> CheckIfDatabaseExists(string SQLServer, string backupRestore) 
{ 
    bool result = false; 
    List<string> DBList = new List<string>(); 
    string sqlConnectionString = this.rbWindow.Checked ? 
       "Server=" + this.cboSQLServer.Text.Trim() + ";Database=master;Trusted_Connection=Yes" : 
       "Server=" + this.cboSQLServer.Text.Trim() + ";Database=master;uid=" + this.txtUsername.Text.Trim() + ";pwd=" + this.txtPassword.Text.Trim(); 
    foreach (Control c in groupBox1.Controls) 
    { 
     if (c.GetType() == typeof(TextBox)) 
     { 
      SqlConnection tmpConn = new SqlConnection(sqlConnectionString); 

      string sqlCreateDBQuery = string.Format("SELECT database_id FROM sys.databases WHERE Name = '{0}'", c.Text); 

      using (tmpConn) 
      { 
       using (SqlCommand sqlCmd = new SqlCommand(sqlCreateDBQuery, tmpConn)) 
       { 
        tmpConn.Open(); 

        object resultObj = sqlCmd.ExecuteScalar(); 

        int databaseID = 0; 

        if (resultObj != null) 
        { 
         int.TryParse(resultObj.ToString(), out databaseID); 
        } 

        tmpConn.Close(); 

        result = (databaseID > 0); 
        if ((!result) && (backupRestore == "backup")) 
        { 
         DBList.Add("[" + c.Text + "]"); 
        } 
        else if ((result) && (backupRestore == "restore")) 
        { 
         DBList.Add("[" + c.Text + "]"); 
        } 

       } 
      } 
     } 
    } 
    return DBList; 
} 

按鈕單擊

private void btnRestore_Click(object sender, EventArgs e) 
{ 
    string outputFolder = this.txtFolder.Text; 
    if (string.IsNullOrEmpty(outputFolder)) 
    { 
     MessageBox.Show("Please select source folder!", "Empty Source Folder", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
    else 
    { 
     List<string> ExistDB = new List<string>(); 
     ExistDB = this.CheckIfDatabaseExists(this.cboSQLServer.Text, "restore"); 
     if (ExistDB.Count == 0) 
     { 
      RestoreDatabase(this.cboSQLServer.Text, this.txtFolder.Text); 
     } 
     else 
     { 
      MessageBox.Show("Databases " + string.Join(", ", ExistDB) + " exist!", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 
} 

恢復DB代碼

公共無效RestoreDatabase(字符串SQLSERVER,串outputFolder) {

 ServerConnection con = new ServerConnection(SQLServer); 
     Server server = new Server(con);    

      foreach (Control c in groupBox3.Controls) 
      { 

       //try 
       //{ 
        if (c.GetType() == typeof(TextBox)) 
        { 
         Restore destination = new Restore(); 
         destination.Action = RestoreActionType.Database; 
         destination.Database = c.Text; 
         string backUpFile = outputFolder + "\\" + destination.Database + ".bak"; 
         BackupDeviceItem source = new BackupDeviceItem(backUpFile, DeviceType.File); 

         string logFile = Path.GetDirectoryName(backUpFile); 
         logFile = Path.Combine(logFile, destination.Database + "_Log.ldf"); 

         string dataFile = Path.GetDirectoryName(backUpFile); 
         dataFile = Path.Combine(dataFile, destination.Database + ".mdf"); 


         destination.Devices.Add(source); 
         DataTable logicalRestoreFiles = destination.ReadFileList(server); 
         destination.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[0][0].ToString(), dataFile)); 
         destination.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[1][0].ToString(), logFile)); 
         destination.ReplaceDatabase = true; 
         destination.SqlRestore(server); 
        }    
       //} 
       //catch (Exception ex) 
       //{ 
       //MessageBox.Show(ex.Message); 
       //}                
      }  
      }     

這是引發異常的代碼 2

錯誤說:

「無法打開備份設備'D:\ TestBackup \ VSM642SP2QC__VfsWorkflow.bak'。 ?>操作系統錯誤2(系統無法找到文件>指定)。\ r \ nRESTORE FILELIST正在異常終止。」

我應該怎麼辦?

+0

當其失敗;在第一個數據庫或之後? – Anil

+0

這很奇怪。我從來沒有使用過這些類。但是使用Sql Server命令而不是那個呢?只需執行命令「從磁盤恢復[數據庫名稱] = [備份文件位置] –

+0

您聲明設備類型(源),但絕不會將其掛接到您的Restore對象。調用此方法之前的DeviceType。 「 –

回答

2

加入這行代碼,立即在您使用ReadFileList

destination.Devices.Add(source); 

還原調用ReadFileList方法之前實例必須聲明DeviceType前行。否則,會拋出異常。你被宣告DeviceType,但從來沒有迷上它給你的Restore

+0

明天我會試試,我把我的工作筆記本電腦留在辦公室,謝謝你的建議:) – Jesse

+0

謝謝@理查德漢塞爾,現在有用! – Jesse

+0

Hi @Richard Hansell。I確實有另一個問題,你可以請審查和問題,並給我的建議? – Jesse

相關問題