Loading ...

SQL Server Management Objects (SMO): How to create backup of a database | CodeAsp.Net

SQL Server Management Objects (SMO): How to create backup of a database

0 (0votes)

If you are using SQL Server Management Objects (SMO) and want to take backup of a database the following code will help you:

 private static void BackupDatabase(string dbName, string backupPath)
            // Fetch the server name from web.config connection string.
            string serverName = new SqlConnection(SQLHelper.GetConnectionString()).DataSource;

            // Create the Backup SMO object to manage the execution
            Backup backup = new Backup();

            // Add the file to backup to
            backup.Devices.Add(new BackupDeviceItem(backupPath, DeviceType.File));

            // Set the name of the database to backup
            backup.Database = dbName;

            // Tell SMO that we are backing up a database
            backup.Action = BackupActionType.Database;

            backup.Incremental = false;

            // Specify that the log must be truncated after the backup is complete.
            backup.LogTruncation = BackupTruncateLogType.Truncate;

            // Begin execution of the backup

            // Waits for the current backup operation to complete before moving on to the next process.

Namespace used:

using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

The Backup class used above has:

Namespace: Microsoft.SqlServer.Management.Smo
Assembly: Microsoft.SqlServer.SmoExtended (in Microsoft.SqlServer.SmoExtended.dll)

Comments (no comments yet)

Top Posts