Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8691581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:08:51+00:00 2026-06-13T00:08:51+00:00

I’m using the following SMO code trying to restore a SQL Server database: Server

  • 0

I’m using the following SMO code trying to restore a SQL Server database:

Server _server;
ServerConnection _conn;

public void Restore(string destinationPath)
{
   Restore res = new Restore();
   _conn = new ServerConnection { ServerInstance = "." };
   _server = new Server(_conn);

   try
   {
       string fileName = destinationPath;
       const string databaseName = "RelationAtOffice";

       res.Database = databaseName;
       res.Action = RestoreActionType.Database;
       res.Devices.AddDevice(fileName, DeviceType.File);

       res.PercentCompleteNotification = 10;
       res.ReplaceDatabase = true;
       res.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
       res.SqlRestore(_server);

       System.Windows.Forms.MessageBox.Show("Restore of " + databaseName + " Complete!", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (SmoException exSMO)
    {
       System.Windows.Forms.MessageBox.Show(exSMO.ToString());
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.ToString());
    }
}

Why the code the following link to the correct answer. But my code is not working?
Code like together. I used the wpf and following link user the winapp

Link

SMO error :

Microsoft.SqlServer.Management.Smo.FailedOperationException: Restore
failed for Server ‘MORTEZA’.
—> Microsoft.SqlServer.Management.Smo.SmoException:
System.Data.SqlClient.SqlError: Exclusive access could not be obtained
because the database is in use.
at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQueryWithMessage(StringCollection
queries, ServerMessageEventHandler dbccMessageHandler, Boolean
errorsAsMessages)
at Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(Server
server, StringCollection queries)
at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv)
at RelationAtOfficeApp.Admin.AdministratorMainPage.Restore(String
destinationPath) in
E:\prozhe\RelationAtOfficeApp\RelationAtOfficeApp\RelationAtOfficeApp\Admin\AdministratorMainPage.xaml.cs:line 268

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:08:52+00:00Added an answer on June 13, 2026 at 12:08 am

    The most likely cause is this:

    • you’ve taken a backup from your server – backed up the Data.mdf and Data_Log.ldf into a Backup.bak file

    • on the same (server) machine, you’re now trying to restore that same database

    In this case, the .mdf and .ldf should be overwritten – but that’s not going to happen, because SQL Server still has that database under its control – so the restore fails, since the data and log file cannot be overwritten.

    There’s two ways you can solve this:

    1. define “file relocations”, e.g. define a new data and log file name upon restore. That way, your database now is restored, and the files are placed in the SQL Server data directory under a new name.

      This requires code something like his:

      ....  
      res.Devices.AddDevice(fileName, DeviceType.File);
      
      // define "file relocation" - for all "logical" files in a SQL Server database,
      // define a new physical location where the file will end up at          
      RelocateFile relocateDataFile = new RelocateFile("Data", @"(your data dir)\RestoredData.mdf");
      RelocateFile relocateLogFile = new RelocateFile("Log", @"(your log dir)\Data\RestoredData_log.ldf");
      
      res.RelocateFiles.Add(relocateDataFile);
      res.RelocateFiles.Add(relocateLogFile);
      
    2. If you don’t want to create new files, you should be able to specify to SMO Restore that you want to replace the existing database with the restore operation, by setting:

      res.ReplaceDatabase = true;
      

      before calling

      res.SqlRestore(_server);
      

    Update: the logical file names that I used in the sample are of course just samples – in your case, your backup file most likely will contain other logical file names, and you need to use those file names in your code.

    You can find out what logical file names your database contains basically two ways:

    1. as the error message clearly stated – you can use the FILELISTONLY option on the RESTORE command in SQL Server Management Studio, thus inspecting the .bak file before you restore it. Try something like this:

      RESTORE FILELISTONLY
      FROM DISK = N'path-and-full-file-name-of-your-bak-file-here.bak'
      

      This will give you a small grid with the logical and physical file names contained in your .bak file.

    2. If you have the database still attached to your server, you can use the Object Explorer in SQL Server Management Studio to find out what the logical file names of your database are; right-click on your database of choice and then you’ll see this dialog box, and in the Files section, you get the information you’re looking for:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.