【问题标题】:SMO copy table structure but not defaultsSMO 复制表结构,但不是默认值
【发布时间】:2015-03-24 15:58:56
【问题描述】:

我正在尝试使用 Transfer 对象复制表(结构而非数据)。我不想复制列中的默认值,因为它们具有我不想在其中引用的存储过程,因此会使传输崩溃。

谁能告诉我如何复制表而不是它们的默认约束?

        Transfer transfer = new Transfer(sourceDatabase);
        transfer.CopyAllObjects = false;
        transfer.CopyData = false;
        transfer.CopyAllTables = false;
        transfer.DestinationDatabase = destinationDatabase.Name;
        transfer.DestinationServer = sourceServer.Name;

        foreach (Table sourceTable in sourceDatabase.Tables)
        {
            transfer.ObjectList.Add(sourceTable);
        }

        transfer.TransferData();

谢谢

【问题讨论】:

  • Sql结束你可以使用这个select * into newtable from existingtable where 1=0

标签: c# sql sql-server smo


【解决方案1】:

此代码将仅复制带有主键、唯一键和索引的表定义,但表之间没有任何关系。

Transfer transfer = new Transfer(sourceDatabase);   

transfer.DestinationLoginSecure = sourceServer.DestinationLoginSecure;
transfer.DestinationLogin = sourceServer.ConnectionContext.Login;
transfer.DestinationPassword = sourceServer.ConnectionContext.Password;

transfer.DestinationDatabase = destinationDatabase.Name;
transfer.DestinationServer = sourceServer.Name;

transfer.CopyAllObjects = false;
transfer.CopyAllTables = false;
transfer.CopySchema = true;
transfer.DropDestinationObjectsFirst = true;

transfer.Options.WithDependencies = false;
transfer.Options.ContinueScriptingOnError = true;
transfer.Options.DriAll = false;
transfer.Options.DriDefaults = false;
transfer.Options.DriIndexes = true;
transfer.Options.DriPrimaryKey = true;
transfer.Options.DriUniqueKeys = true;
transfer.Options.DriForeignKeys = false;
transfer.Options.IncludeIfNotExists = true;

foreach (Table sourceTable in sourceDatabase.Tables)
{
    transfer.ObjectList.Add(sourceTable);
}

transfer.TransferData();

【讨论】:

    猜你喜欢
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2013-02-24
    • 2010-10-19
    • 2014-01-17
    相关资源
    最近更新 更多