2014-07-17 41 views
1

我的問題是我無法使用C#代碼在wpf中恢復我的數據庫。錯誤是'服務器'DELL/LOCALSERVER恢復失敗'。我剛剛發現我正在使用的代碼,它是這一個:使用C#代碼恢復失敗

//用於獲取.bak文件路徑

private void btnBrowse_Click(object sender, RoutedEventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
    ofd.ShowDialog(); 
    txtBackupPath.Text = ofd.FileName; 
} 

private void btnRestore_Click(object sender, RoutedEventArgs e) 
{ 
    ServerConnection con = new ServerConnection(@server, userId, password); 
    Server sqlServerInstance = new Server(con); 
    Restore destination = new Restore(); 
    destination.Action = RestoreActionType.Database; 
    destination.Database = initial_catalog; 
    BackupDeviceItem source = new BackupDeviceItem(txtBackupPath.Text, DeviceType.File); 
    destination.Devices.Add(source); 
    destination.ReplaceDatabase = true; 
    destination.SqlRestore(sqlServerInstance); 
    System.Windows.Forms.MessageBox.Show("The restore of database " + "'H2RPDB'" + " completed sccessfully", "Microsoft SQL Server Management Studio", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    con.Disconnect(); 
} 

我不知道什麼是錯的是新的這個代碼。感謝您的答案。

+0

**哪裏**你會得到這個錯誤,在哪行代碼? –

+0

我從destination.SqlRestore(sqlServerInstance)中得到這個錯誤; – psyche

+0

您是否知道它是打開文件的服務器,因此從服務器的角度來看,它的路徑必須有效?即'C:\ test.bak'在服務器C:磁盤上。 –

回答

1

幸運的是代碼是正確的。只是系統沒有權限訪問我嘗試訪問的路徑。除此之外,它的作品。