Question: I'm restoring a database from a file named `databasebkup.bak` (saved in `C:\\databasebkup.bak`) to a Sql Server 2005 database named MyDatabase.

I've referenced `Microsoft.SqlServer.Management.Common;Microsoft.SqlServer.Management.Sdk.Sfc;`
and `Microsoft.SqlServer.Management.Smo` in my code, which looks like this:

```
Microsoft.SqlServer.Management.Smo.Server smoServer = new Server(
                                                        new ServerConnection("."));
Database db = smoServer.Databases["MyDatabase"];
string dbPath = Path.Combine(db.PrimaryFilePath, "MyDatabase.mdf");
string logPath = Path.Combine(db.PrimaryFilePath, "MyDatabase_Log.ldf");

Restore restore = new Restore();
var deviceItem = new BackupDeviceItem("C:\\databasebkup.bak", DeviceType.File);

restore.Devices.Add(deviceItem);
restore.Database = "C:\\databasebkup.bak";

//restore.FileNumber = restoreFileNumber;
restore.Action = RestoreActionType.Database;
restore.ReplaceDatabase = true;
restore.SqlRestore(smoServer);

db.SetOnline();
smoServer.Refresh();
db.Refresh();

```

But while restoring, it returns this error:

> 
> Restore failed for Server servername'
> 
> 
> 

Can anyone tell me where I'm going wrong?

Answer: 
Try changing:

```
restore.Database = "C:\\databasebkup.bak";

```

to:

```
restore.Database = "MyDatabase";

```

Also, it looks like you're connecting using Windows Authentication; make sure the currently logged in user has the proper rights to do a backup.
You put your backup file name in the wrong property, and completely forgot to specify the database name itself. Read the documentation for the `Database` and `MediaName` properties again.

<http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.restore.aspx>