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 1008227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:44:23+00:00 2026-05-16T08:44:23+00:00

i have some project, which can be running on databases with different names. I

  • 0

i have some project, which can be running on databases with different names. I have some functionality in my program to do database backups and to restoring from them. The problem is when i try to restore database from backup to other database with a different name and so on. My code looks like this:

        public void Restore(string backupFilePath, string toDatabase)
    {
        // Kill database if exists
        Database db = server.Databases[toDatabase];
        RecoveryModel recoverymod = RecoveryModel.Full; 
        if (db != null)
        {
            recoverymod = db.DatabaseOptions.RecoveryModel;
            server.KillAllProcesses(toDatabase);
        }

        // Restore to database
        var bdi = new BackupDeviceItem(backupFilePath, DeviceType.File);
        var restore = new Restore();
        restore.Devices.Add(bdi);
        restore.FileNumber = restore.Devices.IndexOf(bdi);
        restore.Database = toDatabase;
        restore.KeepReplication = true;
        restore.Action = RestoreActionType.Database;
        restore.ReplaceDatabase = true;
        //restore.Restart = true;
        restore.NoRecovery = false;
        string dbFolder = Path.Combine(server.InstallDataDirectory, "DATA");

        string backupDBName = toDatabase; //Path.GetFileName(backupFilePath).Split('_')[0]; 
        restore.RelocateFiles.Add(new RelocateFile(backupDBName, Path.Combine(dbFolder, toDatabase + ".MDF")));
        restore.RelocateFiles.Add(new RelocateFile(backupDBName + "_log",
                                                   Path.Combine(dbFolder, toDatabase + ".LDF")));
        restore.SqlRestore(server);

        // Startup database
        db = server.Databases[toDatabase];
        db.DatabaseOptions.RecoveryModel = recoverymod; 
        db.SetOnline();
        while (((int)db.Status & (int)DatabaseStatus.Normal) != (int)DatabaseStatus.Normal)
        {
            Thread.Sleep(1000);
        }
    }

But SQL outputs errors like these:

Msg 3234, Level 16, State 2, Line 1
Logical file 'ServeDB7' is not part of database 'ServeDB7'. Use RESTORE FILELISTONLY to list the logical file names.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

I think this is becouse backups can be made from databases with different logical names, such as “Serve”, “Serve2”, “serve3” or something like this.

OR it puts some other error, which says, that file cannot be moved, becouse other database are using it.
Maybe Is there some way to define a database name (like “temporaryDB”) for temporary DB, from which files later could be replaced on a original databas? Maybe my code is somewhere incorect, could anyone help?

  • 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-05-16T08:44:24+00:00Added an answer on May 16, 2026 at 8:44 am

    I solved this by leaving existing database name, like this:

    string backupDBName = GetDbNameFromBackup(restore);
                restore.RelocateFiles.Add(new RelocateFile(backupDBName, Path.Combine(dbFolder, toDatabase + ".MDF")));
                restore.RelocateFiles.Add(new RelocateFile(backupDBName + "_log", Path.Combine(dbFolder, toDatabase + ".LDF")));
                restore.SqlRestore(server);
    
    private string GetDbNameFromBackup(Restore restore)
            {
                string originalDbName = null;
                if (restore != null && this.server != null)
                {
                    DataTable restoreInfo = restore.ReadFileList(this.server);
                    if (restoreInfo.Rows.Count > 0)
                    {
                        object[] cols = restoreInfo.Rows[0].ItemArray;
                        if (cols != null && cols.Count() > 0)
                        {
                            if (cols[0] is string && !String.IsNullOrEmpty((string)cols[0]))
                            {
                                originalDbName = (string)cols[0];
                            }
                        }
                    }
                }
                return originalDbName;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have copied some files from project A to project B, both of which
I have a Django-project which was running fine, until I made some changes I
I have a project which can only be deployed on a server running .NET
I have some issues in setting the classpath for the a project for which
I have to test some things on a project which has been developed by
I have a project which has some lib's associated with it (in Build Path).
I have a class library project which contains some content files configured with the
I have a windows installer project which installs some software (winform, service, mce addin).
I have a console project which I want to start with some parameters argc
I have an ASP.NET project which has already some custom fields in it's web.config

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.