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
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
The most likely cause is this:
you’ve taken a backup from your server – backed up the
Data.mdfandData_Log.ldfinto aBackup.bakfileon the same (server) machine, you’re now trying to restore that same database
In this case, the
.mdfand.ldfshould 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:
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:
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:
before calling
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:
as the error message clearly stated – you can use the
FILELISTONLYoption on theRESTOREcommand in SQL Server Management Studio, thus inspecting the.bakfile before you restore it. Try something like this:This will give you a small grid with the logical and physical file names contained in your
.bakfile.If you have the database still attached to your server, you can use the
Object Explorerin 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 theFilessection, you get the information you’re looking for: