使用 SMO,以下效果很好!并在约 40 秒内复制。
private static void CopyDBSchema(string sourceDBServer, string sourceDBName, string targetDBServer, string targetDBName)
{
Server server = new Server(sourceDBServer);
server.SetDefaultInitFields(true);
Database db = server.Databases[sourceDBName];
Transfer t = new Transfer()
{
Database = db,
CopyData = false,
CopyAllObjects = false,
CopyAllUsers = false,
CopyAllLogins = false,
CopySchema = true,
CopyAllTables = true,
CopyAllViews = true,
CopyAllStoredProcedures = true,
CopyAllDatabaseTriggers = true,
CopyAllUserDefinedAggregates = true,
CopyAllUserDefinedDataTypes = true,
CopyAllUserDefinedFunctions = true,
CopyAllUserDefinedTypes = true,
DropDestinationObjectsFirst = true,
UseDestinationTransaction = true,
DestinationServer = targetDBServer,
DestinationDatabase = targetDBName
};
t.Options.WithDependencies = true;
t.Options.IncludeIfNotExists = true;
t.Options.DriPrimaryKey = true;
t.Options.DriForeignKeys = true;
t.Options.DriIndexes = true;
t.Options.DriNonClustered = true;
t.Options.DriUniqueKeys = true;
t.Options.DriAllKeys = true;
t.Options.Triggers = true;
t.TransferData();
server = null;
}