【问题标题】:How to copy tables base on schema name in Microsoft.SqlServer.Management.Smo.Transfer如何根据 Microsoft.SqlServer.Management.Smo.Transfer 中的架构名称复制表
【发布时间】:2015-04-30 10:04:49
【问题描述】:

我正在从 Source_Test 数据库 Destination_Test 数据库中复制表。

使用Microsoft.SqlServer.Management.Smo.Transfer

它复制了所有的表from Source_Test to Destination_Test

但我想要具有架构“TestSchema”的表。

主要问题是,它复制了所有具有dbo schema 的表。即使我设置了this.CopyAllTables = false;,它仍然会复制它。如何限制它。 我试过以下:

public class CopyTable : Transfer, ISchemaCopy
{
   private void CopyTables()
   {
      this.CopyAllTables = false;
      var server = new Server(new ServerConnection(connection));
      var script = this.ScriptTransfer().Cast<string>();
   }
}

【问题讨论】:

    标签: c# sql sql-server smo


    【解决方案1】:

    我认为下面的代码可以实现您想要实现的目标。如果通过提供 SchemaName 将其设置为仅从一个模式复制表,它将仅添加该模式的表进行复制。

    关键是建立一个表集合,这些表是您要复制的表,并将该集合显式添加到 ObjectList。不要忘记将 ServerName 和 DatabaseName 更改为真实的东西。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Common;
    
    /* Needs references to:
     * 
     * System.Data
     * Microsoft.SqlServer.ConnectionInfo 
     * Microsoft.SqlServer.Management.Sdk.Sfc
     * Microsoft.SqlServer.Smo
     * Microsoft.SqlServer.SmoExtended
     */
    
    namespace CopyTablesExample
    {
        class Program
        {
            public class CopyTables : Transfer
            {
                public string SchemaName { get; set; }
                public string DatabaseName { get; set; }
                public string ServerName { get; set; }
    
    
                public IEnumerable<string> GetScript()
                {
                    //This is what we will return
                    IEnumerable<string> scriptStrings = null;
    
                    if (this.DatabaseName == null) {
                        throw new Exception("DatabaseName property not set.");
                    }
    
                    if (this.ServerName == null) {
                        throw new Exception("ServerName property not set.");
                    }
    
                    var server = new Server(new ServerConnection(ServerName));
                    this.Database = server.Databases[this.DatabaseName];
    
                    //Turn off all objects.  Below we will start turning on what we want.
                    //You may wish to add more object types.
                    this.CopyAllObjects = false;
    
                    if (this.SchemaName == null) {
                        //No schema means all tables
                        this.CopyAllTables = true;
                    }
                    else {
                        //A specific schema means all tables from that schema
                        this.CopyAllTables = false;
                        //We only want to copy tables in a specific schema.
                        List<Table> tablesToCopy = new List<Table>();
                        foreach (Table t in this.Database.Tables) {
                            if (t.Schema.Equals(this.SchemaName)) {
                                tablesToCopy.Add(t);
                            }
                        }
    
                        //Add specifically the tables we want which are from the schema we want
                        this.ObjectList.AddRange(tablesToCopy);
                    }
    
                    try {
                        scriptStrings = this.ScriptTransfer().Cast<string>();
                    }
                    catch (Exception ex) {
                        Console.WriteLine("We got an exception.");
                        throw;
                    }
                    return scriptStrings;
                }
            }
    
            static void Main(String[] Args)
            {
                //Only set a SchemaName in line below, when you want to restrict yourself to copying that schema's tables.
                CopyTables ct = new CopyTables() { ServerName = "xyz", DatabaseName = "abc", SchemaName = "junk" } ; 
                var copyTablesScript = ct.GetScript();
    
                //For validation, display the script as generated
                foreach (var item in copyTablesScript) {
                    Console.WriteLine(item);
                }
                var discard = Console.ReadKey();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-28
      • 2011-11-25
      • 2012-02-04
      • 1970-01-01
      • 2011-07-18
      • 2023-03-16
      • 1970-01-01
      • 2019-04-24
      • 2013-09-14
      相关资源
      最近更新 更多