1

My problem is that I can't restore my database in wpf using C# code. The error is 'Restore failed for Server 'DELL/LOCALSERVER'. I just found the code I'm using and it's this one:

//for getting .bak file path

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();
}

I don't know what's wrong being new to this code. Thanks for the answers.

4
  • WHERE do you get this error, on what line of code? Commented Jul 17, 2014 at 7:08
  • I get this error from destination.SqlRestore(sqlServerInstance); Commented Jul 17, 2014 at 7:37
  • Are you aware of the fact that it is the server that is opening the file and thus the path to it has to be valid from the servers perspective? Ie C:\test.bak is on the servers C: disk. Commented Jul 17, 2014 at 7:40
  • So, I can't restore or even backup a database in other path like for example in Desktop? Commented Jul 17, 2014 at 7:57

1 Answer 1

1

Fortunately the code is correct. It's just that the system doesn't have any permission to access the path I'm trying to access. Other than that, it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.