2011年8月23日 星期二

Backup SQL Database SMO

參考
--
[VB.NET CODE STARTS]

'--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
.Devices.Add(bkDevItem)
.Initialize = True
.Checksum = True
.ContinueAfterError = True
.Incremental = False
.LogTruncation = BackupTruncateLogType.Truncate
.SqlBackup(oSQLServer) ' backup SQL database
End With

End Sub

[VB.NET CODE ENDS]

沒有留言:

張貼留言