Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Restore restore = new Restore();
	    restore.Database = "bw_restore";
            restore.Action = RestoreActionType.Database;

            restore.Devices.AddDevice(@"C:\SQLBackup\bw.bak",DeviceType.File);
             restore.ReplaceDatabase = true;
            
            restore.NoRecovery = false;
            
            restore.RelocateFiles.Add(new RelocateFile("bw",@"C:\SQLBackup\bw.mdf"));
            restore.RelocateFiles.Add(new RelocateFile("bw_Log",@"C:\SQLBackup\bw_Log.ldf"));
            ServerConnection connection = new ServerConnection(@"Data Source=NILE-ROSE\SQLEXPRESS;User ID=sa;Password=welcome@123;Initial Catalog=bw_restore;");
            
            connection.LoginSecure = true;
            //connection.LoginSecure = false;
            //connection.Login = "testuser";
            //connection.Password = "testuser";
            Server sqlServer = new Server(connection);
            //MessageBox.Show(sqlServer.ToString());
            restore.SqlRestore(sqlServer);
            MessageBox.Show("Restored Successfully");



Getting Error Like
An unhandled exception of type 'Microsoft.SqlServer.Management.Smo.FailedOperationException' occurred in Microsoft.SqlServer.SmoExtended.dll
Posted
Updated 6-Sep-12 23:54pm
v2

1 solution

sqry = "use master" & vbCrLf & "RESTORE DATABASE " & databasename & " FROM DISK = '" & path where database is kept & "' WITH replace,move 'databasename ' to '" & path for mdf + databasename & ".mdf', MOVE 'databasename _log' to '" & path for ldf + databasename & "_log.ldf'" & vbCrLf & "SET LOCK_TIMEOUT 3000"
' MsgBox(SQL)
da = New SqlDataAdapter(sqry, mycon)
da.SelectCommand.ExecuteNonQuery()
Catch ex As Exception

End Try
 
Share this answer
 
Comments
Umapathi K 7-Sep-12 6:14am    
str = @"RESTORE DATABASE bw FROM DISK = 'c:\SQLBackup\bw.bak' WITH MOVE 'bw' TO 'c:\SQLBackup\bw.mdf' , MOVE 'bw_Log' TO 'C:\SQLBackup\bw.ldf', REPLACE ";

this is what i written ., but still error

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