我正在尝试使用 SQL Server 管理对象 (SMO)、 Transfer类创建表的副本(没有数据,只有架构) 。我唯一没有弄清楚的是如何指定要复制到的服务器,当服务器位于不同的主机上时。就我而言,我想从 10.1.2.x 复制到 10.1.2.y。有没有办法指定这个,或者这个类不支持它?
也许有更好的 C# 解决方案?
static void CreateTableFromTable(string fromConnection, string toConnection, string dbName, string tablename, bool copyData = false)
{
Server fromServer = new Server(new ServerConnection(new SqlConnection(fromConnection)));
Database db = fromServer.Databases[dbName];
Transfer transfer = new Transfer(db);
transfer.CopyAllObjects = false;
transfer.DropDestinationObjectsFirst = false;
transfer.CopySchema = false; //Database schema? Or Table schema? I DO NOT want to overwrite the db schema
transfer.CopyData = copyData;
transfer.DestinationServer = "?";
transfer.DestinationDatabase = dbName;
transfer.Options.IncludeIfNotExists = true;
transfer.ObjectList.Add(db.Tables[tablename]);
transfer.TransferData();
}