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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:19:29+00:00 2026-06-04T07:19:29+00:00

I am restoring a SQL database using C# code.When we are restore a backup

  • 0

I am restoring a SQL database using C# code.When we are restore a backup using SQL server management studio we can chose the back up set.Can we do that using the code.
enter image description here

I used the following code

openFileDialog1.ShowDialog();
    string databaseName = "TempDb";
    Restore sqlRestore = new Restore();

    BackupDeviceItem deviceItem = new BackupDeviceItem(openFileDialog1.FileName, DeviceType.File);
    sqlRestore.Devices.Add(deviceItem);
    sqlRestore.Database = databaseName;

    DataConnection dataConnection = new DataConnection();
    ServerConnection connection = new ServerConnection(dataConnection.DataBaseConnection);
    Server sqlServer = new Server(connection);

    Database db = sqlServer.Databases[databaseName];
    sqlRestore.Action = RestoreActionType.Database;
    String dataFileLocation = db.FileGroups[0].Files[0].FileName;
    String logFileLocation = db.LogFiles[0].FileName;
    db = sqlServer.Databases[databaseName];
    sqlServer.ConnectionContext.Disconnect();
    RelocateFile rf = new RelocateFile(databaseName, dataFileLocation);


    sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFileLocation));
    sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName + "_log", logFileLocation));
    sqlRestore.ReplaceDatabase = true;
    sqlRestore.Complete += new ServerMessageEventHandler(sqlRestore_Complete);
    sqlRestore.PercentCompleteNotification = 10;
    sqlRestore.PercentComplete += new PercentCompleteEventHandler(sqlRestore_PercentComplete);
    sqlRestore.SqlRestore(sqlServer);
    db = sqlServer.Databases[databaseName];
    db.SetOnline();
    sqlServer.Refresh();

Using the above code its only restore the first set.

  • 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-04T07:19:29+00:00Added an answer on June 4, 2026 at 7:19 am

    Taking a look at Use all backup sets to restore database with SMO, it appears that

    The important field is FileNumber on the Restore object. The default value is 1

    You can get a list of Backupsetsusing the Restore.ReadBackupHeader method.

    VB code from the link

    res.Devices.AddDevice("C:\AdventureWorks2012Backup.BAK", DeviceType.File)
    dt = res.ReadBackupHeader(srv)
    
    For Each r As DataRow In dt.Rows
       For Each c As DataColumn In dt.Columns
          Console.WriteLine(c.ToString + " = " + r(c).ToString())
       Next
    Next
    

    So once you know which backupset you want you can set

    sqlRestore.FileNumber = YOUR_CHOSEN_SET;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to restore a database by first restoring a full backup and
When restoring a full DB backup to a different SQL Server 2005 Express server,
I'm trying to simulate some code that I have working with SQL but using
We're storing some Guid's in a MS SQL database. There's some legacy code that
I have a huge SQL file that I want to restore to mysql server
I have a WCF service, a MS SQL database, and the Visual Studio 2008
I am getting following error. Restore failed for Server I have recently upgraded SQL
I have created an Amazon EC2 Instance that provides Windows Server 2008 with SQL
I have a website running on a Windows 2008 R2 server, using a SQL
I use EF Code First in my solution with SQL Server Express behind it.

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.