【问题标题】:SQL Database backup using SMO and VB.NET使用 SMO 和 VB.NET 备份 SQL 数据库
【发布时间】:2012-03-25 06:41:46
【问题描述】:

我正在尝试运行这段看似简单的代码,它在整个网络上重复了几次,但我在代码中遇到了错误:

Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common

Private Sub BackupDatabaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackupDatabaseToolStripMenuItem.Click
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
'Store the current recovery model in a variable.
Dim recoverymod As Integer
recoverymod = db.DatabaseOptions.RecoveryModel
'Define a Backup object variable. 
Dim bk As New Backup
'Specify the type of backup, the description, the name, and the database to be backed up.
bk.Action = BackupActionType.Database
bk.BackupSetDescription = "Full backup of AdventureWorks2008R2"
bk.BackupSetName = "AdventureWorks2008R2 Backup"
bk.Database = "AdventureWorks2008R2"
'Declare a BackupDeviceItem by supplying the backup device file name in the constructor, and the type of device is a file.
Dim bdi As BackupDeviceItem
bdi = New BackupDeviceItem("Test_Full_Backup1", DeviceType.File)
'Add the device to the Backup object.
bk.Devices.Add(bdi)
'Set the Incremental property to False to specify that this is a full database backup.
bk.Incremental = False
'Set the expiration date.
Dim backupdate As New Date
backupdate = New Date(2006, 10, 5)
bk.ExpirationDate = backupdate
'Specify that the log must be truncated after the backup is complete.
bk.LogTruncation = BackupTruncateLogType.Truncate
'Run SqlBackup to perform the full database backup on the instance of SQL Server.
bk.SqlBackup(srv)
End Sub

我在以下几行遇到错误: 将 bk 调暗为新备份

错误 5 类型“备份”未定义。

bk.Action = BackupActionType.Database

错误 6 'BackupActionType' 未声明。由于其保护级别,它可能无法访问。

将 bdi 调暗为 BackupDeviceItem

错误 7 类型“BackupDeviceItem”未定义。

还有其他类似的错误

我错过了一些“进口”吗? 我从这里得到代码:http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.backup.sqlbackup.aspx

【问题讨论】:

    标签: vb.net sql-server-2008 smo


    【解决方案1】:

    您需要添加对以下程序集的引用(不仅仅是导入)

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多