(非原创,资料来源于培训资料)
1、引用 Microsoft.SqlServer.ConnectionInfo和Microsoft.SqlServer.Smo;
2、连接SQLServer服务器
Dim myServer As New Server()
Dim conn As ServerConnection
'##### Add connection code here #####
conn = myServer.ConnectionContext
conn.ServerInstance = "localhost"
conn.Connect()
Dim conn As ServerConnection
'##### Add connection code here #####
conn = myServer.ConnectionContext
conn.ServerInstance = "localhost"
conn.Connect()
3、设置必要的属性,包括:备份动作、备份名称 、要备份的数据库和备份设备。然后开始备份.
Dim MyBackup As New Backup
MyBackup.Action = BackupActionType.Database
MyBackup.BackupSetName = dbName & "Backup"
MyBackup.Database = dbName '备份的数据库名称
Dim deviceItem As BackupDeviceItem = New BackupDeviceItem("", DeviceType.File)
deviceItem.Name = "C:\" & dbName & ".bak"
MyBackup.Devices.Add(deviceItem)
MyBackup.SqlBackup(myServer)
MyBackup.Action = BackupActionType.Database
MyBackup.BackupSetName = dbName & "Backup"
MyBackup.Database = dbName '备份的数据库名称
Dim deviceItem As BackupDeviceItem = New BackupDeviceItem("", DeviceType.File)
deviceItem.Name = "C:\" & dbName & ".bak"
MyBackup.Devices.Add(deviceItem)
MyBackup.SqlBackup(myServer)