Click here to Skip to main content
15,916,030 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone! I restore the database, but I'm having one error. This error is "Restore failed for Server WIN_64-PC\SQLEXPRESS" How do I rectify this error ?
Here is my code
C#
public void Restore(string Servername, string DBname, string user, string pass, string path)
        {
            try
            {
                Restore restore = new Restore();
                restore.Database = DBname;
                restore.Action = RestoreActionType.Database;


                BackupDeviceItem deviceitem = new BackupDeviceItem(path, DeviceType.File);


                ServerConnection connection = new ServerConnection(Servername, user, pass);
                Server server = new Server(connection);

                restore.Devices.Add(deviceitem);

                restore.ReplaceDatabase = true;
                restore.NoRecovery = true;

                restore.Complete += new ServerMessageEventHandler(Restore_Complete);
                restore.PercentComplete += new PercentCompleteEventHandler(CompletionsStatusInPercent);
                
                // perform  the restore
                restore.SqlRestore(server);

            }
            catch (SmoException ex)
            {
                MessageBox.Show(ex.Message);
            }
             catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        void Restore_Complete(object sender, ServerMessageEventArgs e)
        {
            MessageBox.Show("Restore Complete !");
        }
        void CompletionsStatusInPercent(object sender, PercentCompleteEventArgs args)
        {
            Console.WriteLine(args.Percent);
        }
    }
Posted
Updated 21-Aug-13 18:12pm
v4
Comments
TryAndSucceed 21-Aug-13 12:08pm    
If I am not wrong, you said you want to restore Database but you are actually trying to restore Server.

See restore.SqlRestore(server)
vunguyen112 21-Aug-13 23:36pm    
Can you help me restore the database ?
thatraja 21-Aug-13 12:54pm    
Post complete error message(Update in your question)
vunguyen112 21-Aug-13 23:38pm    
This is error "Restore failed for Server Win64_pc/SQLEXPRESS"

1 solution

Hello Vunguyen112,

You can use SMO to achieve this. Following code snippet should get you started.
C#
Microsoft.SqlServer.Management.Smo.Server smoServer = new Server(new ServerConnection(server));

Database db = smoServer.Databases['DB_NAME'];
string dbPath = Path.Combine(db.PrimaryFilePath, 'DBFILE.MDF');
string logPath = Path.Combine(db.PrimaryFilePath, 'DB_LOG.LDF');
Restore restore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem('DRIVE:\MYDATABASE.BAK', DeviceType.File);
restore.Devices.Add(deviceItem);
restore.Database = backupDatabaseTo;
restore.FileNumber = restoreFileNumber;
restore.Action = RestoreActionType.Database;
restore.ReplaceDatabase = true;
restore.SqlRestore(smoServer);

db = smoServer.Databases['DB_NAME'];
db.SetOnline();
smoServer.Refresh();
db.Refresh();

You need following references in your project.

  • Microsoft.SqlServer.Smo
  • Microsoft.SqlServer.SmoExtended
  • Microsoft.SqlServer.Management.Sdk.Sfc

Regards,
 
Share this answer
 
Comments
vunguyen112 22-Aug-13 1:39am    
I tried using your code, but still have the above error!
I do not know why! Please help me!
I am using sql server 2005
Prasad Khandekar 23-Aug-13 7:50am    
Can you post the stack trace?
vunguyen112 23-Aug-13 8:11am    
This error is "An unhandled exception of type 'Microsoft.SqlServer.Management.Smo.FailedOperationException' occurred in Microsoft.SqlServer.Smo.dll"
Prasad Khandekar 23-Aug-13 8:30am    
Use ex.ToString() in your code. It will print more details about the error.
vunguyen112 23-Aug-13 8:50am    
I'm sorry I'm not good at english !
Microsoft.SqlServer.Management.Smo.FailedOperationException: Restore failed for Server 'WIN_64-PC\SQLEXPRESS'. ---> 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)
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv)
at QLDienNuoc.Data_Access_Layer.RestoreDB.Restore(String Servername, String DBname, String user, String pass, String path, String Databasepath, String logpath) in e:\C#\QLDienNuoc\QLDienNuoc\Data Access Layer\RestoreDB.cs:line 59

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900