Software Technology Tips

Using the SQL SMO object we can easily take backup of SQL database through code. Following is the sample code used take backup.


'--Reference added

Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
'--Declare the backup object

Dim WithEvents oBackup As New Backup
'--Method to take backup

Private Sub BackupSqlDatabase()
Dim conn As New ServerConnection(serverName, userName, passWd) ' -- set SQL server connection given the server name, user name and password
Dim oSQLServer As New Server(conn) '--create the SMO server object using connection
Dim OrigBackupPath As String = oSQLServer.Information.MasterDBPath.Replace("\DATA", "\Backup\DB_BACKUP_NAME.BAK") ' -- set the path where backup file will be stored
Dim bkDevItem As New BackupDeviceItem(OrigBackupPath, DeviceType.File) ' -- create SMO.Backupdevice object
With oBackup ' Set the backup object property
         .Action = BackupActionType.Database
         .Database = YOUR_DATABASE_NAME
         .Initialize = True
         .Checksum = True
         .ContinueAfterError = True
         .Incremental = False
         .LogTruncation = BackupTruncateLogType.Truncate
         .SqlBackup(oSQLServer) ' backup SQL database
End With
End Sub
That's it...Backup is created.

Related Tags:

Database, MYSQL, VB.NET, BackUp, SMO

Author: Amrita Dash


Let us Connect!

Awards and Achievements

Red Herringcolor DeloitteFast50 DB ZInnov1   Nascome  DB Stpi

This site uses cookies. We respect your privacy.copyright (c) Mindfire Solutions 2007-2015. Login