Thursday, 20 June 2013 15:46

how to backup sql server database using SMO in asp.net

Written by 
Rate this item
(0 votes)

using SMO classes one can backup a sql server database
in .bak extension.It's a programming approach to backup
database.there are other ways but using SMO classes it
is efficient and error free.

For this first make references of the following in your app -

Microsoft.SqlServer.ConnectionInfo,
Microsoft.SqlServer.Management.Sdk.Sfc,
Microsoft.SqlServer.Smo,
Microsoft.SqlServer.SmoExtended

codes are follows -

ServerConnection conn;
Backup bkp = new Backup();
string subPath = "~/DatabaseBackup";
bool IsExists = System.IO.File.Exists(Server.MapPath(subPath + "\\ConDB.bak"));
if (IsExists) File.Delete(Server.MapPath(subPath + "\\ConDB.bak"));

string databaseName = "mydb";

bkp.Action = BackupActionType.Database;
bkp.Database = databaseName;
bkp.Devices.AddDevice(Server.MapPath(subPath) + "\\ConDB.bak", DeviceType.File);
bkp.Incremental = false;

conn = new ServerConnection(instanceName, userid,password);//put right values of ur server
srv = new Server(conn);

bkp.SqlBackup(srv);//does backup finally

Read 2083 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
More in this category: « how to read from a xml file

1 comment

  • Comment Link aron Thursday, 04 July 2013 21:05 posted by aron

    That's a great post thanks

Login to post comments