如何使用 VB.NET 的 SMO class 将单个 table 传输到 MSSQL 服务器中的另一个数据库?

How can I transfer a single table to another database in MSSQL server using VB.NET's SMO class?

我已经可以创建一个数据库,但是当我使用下面的代码时,它会传输所有 tables 而没有任何记录。此外,我将如何编码仅传输一个特定的 table?

Dim conn As New ServerConnection("NPSS-OJT", "sa", "rms")
        Dim srv As New Server(conn)
        Dim db As Database = srv.Databases("bos_primary_db")
        Dim dbCopy As Database = New Database(srv, Main.dbName)

        Dim xfr As Transfer
        xfr = New Transfer(db)
        xfr.CopyAllTables = True
        xfr.Options.WithDependencies = True
        xfr.Options.ContinueScriptingOnError = True
        xfr.DestinationDatabase = dbCopy.Name
        xfr.DestinationServer = srv.Name
        xfr.DestinationLoginSecure = True
        xfr.CopySchema = True
        xfr.CopyData = True

        xfr.TransferData()

更新代码:

Shared Sub transferTables()
        Dim conn As New ServerConnection("NPSS-OJT", "sa", "rms")
        Dim srv As New Server(conn)
        Dim db As Database = srv.Databases("bos_primary_db")
        Dim dbCopy As Database = New Database(srv, Main.dbName)

        Dim xfr As Transfer
        xfr = New Transfer(db)
        xfr.CopyAllTables = False
        xfr.Options.WithDependencies = False
        xfr.Options.ContinueScriptingOnError = True
        xfr.DestinationDatabase = dbCopy.Name
        xfr.DestinationServer = srv.Name
        xfr.DestinationLoginSecure = True
        xfr.CopySchema = True
        xfr.CopyData = True
        xfr.Options.DriAll = False
        xfr.Options.DriDefaults = True
        xfr.Options.DriIndexes = True
        xfr.Options.DriPrimaryKey = True
        xfr.Options.DriUniqueKeys = True
        xfr.Options.DriForeignKeys = False
        xfr.ObjectList.Add("Accounting")
        xfr.TransferData()
    End Sub

你应该设置

xfr.CopyAllTables = False
xfr.Options.WithDependencies = False

你也应该转移密钥

xfr.Options.DriAll = False 
xfr.Options.DriDefaults = True
xfr.Options.DriIndexes = True
xfr.Options.DriPrimaryKey = True
xfr.Options.DriUniqueKeys = True
xfr.Options.DriForeignKeys = False

然后

编辑(我更改了在传输对象列表中添加 table)

xfr.ObjectList.Add(db.Tables("table_name"))
xfr.TransferData()

要在数据库之间传输指定的对象,您需要将此对象添加到 ObjectList 而不仅仅是对象的名称。