How to restore a DataBase from .bak file using (C#) SMO in DotNet 3.5?

The following code will help you to do this restore processing.

using Microsoft.SqlServer.Management.Smo;

string backupFile="Backupfile";//.bak file

string dbName = "DBName";

string dbServerMachineName = "MachineName";

Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(dbServerMachineName);

Database database = new Database(server, dbName);

//If Need




Restore restore = new Restore();

restore.NoRecovery = false;

restore.Action = RestoreActionType.Database;

BackupDeviceItem bdi = default(BackupDeviceItem);

bdi = new BackupDeviceItem(backupFile, DeviceType.File);


restore.Database = dbName;

restore.ReplaceDatabase = true;

restore.PercentCompleteNotification = 10;







vipendra said...

sir i tried this code for restoring existing database,but it is giving problem during restore.

the Error is :

Restore failed for Server 'ROOM-4T0T2K9RH4\SQLEXPRESS'.

please reply soon..

Arti said...

I have tried this code for VS2005 and Sql Server 2005, but its giving error for Microsoft.SqlServer.Management.Smo.Backup namespace and when i copied Microsoft.SqlServer.SmoExtended.dll giving version error, is it like that this dll is for sql server 2008 and not run for 2005,
please give me solution

Derin said...

Thanks Vinoth, its working for me

bajinder said...

using Microsoft.SqlServer.Management.Smo

name space not found pls give me solution.

EMRE ├ľNDER said...

if you want add using Microsoft.SqlServer.Management.Smo

come to solution explorer and right click to references select add references and select browse tab then select this path C:\programfiles\microsoftSqlServer\100\sdk\assemblies\ you can find in this folder what you want to include.. i perefer connection info , management sdk and two Smo files ;)

Sarvan kumar said...

Restore failed for Server 'ADMIN\SQLEXPRESS'.
for this error is showing..

Unknown said...

How to create and restore database using SMO with version 4.0 and Sql server 2012, in 12 have Version 10/11 and for 3.5 have version 9.0, so this code is not working.