2014-10-17 33 views
2

我想使用Microsoft.SqlServer.Management.Smo将数据库传输到新的数据库,但使用新的模式。如何使用SMO将数据库传输到新模式?

这里就是我此刻做(我打得四处选项来得到我想要的行为,但没有任何的运气):

var source = new Server(GetServerConnection("sa")); 
    var destination = new Server(GetServerConnection("schema_user1")); 

    source.ConnectionContext.Connect(); 
    destination.ConnectionContext.Connect(); 

    var sourceDatabase = source.Databases["support"]; 
    var destinationDatabase = destination.Databases["schema_test"]; 


    var transfer = new Transfer(sourceDatabase) 
    { 
    CopyAllObjects = false, 
    CopyAllUserDefinedDataTypes = true, 
    CopyAllTables = true, 
    CopyData = true, 
    CopyAllStoredProcedures = true, 
    PreserveDbo = false, 
    PreserveLogins = false, 
    DestinationServer = destination.Name, 
    DestinationDatabase = destinationDatabase.Name, 
    DestinationLogin = destination.ConnectionContext.Login, 
    DestinationPassword = destination.ConnectionContext.Password, 
    DestinationLoginSecure = false, 
    Options = {ScriptSchema = false} 
    }; 

    transfer.TransferData(); 

我收到以下错误:

The specified schema name "dbo" either does not exist or you do not have permission to use it. 

这是因为schema_user1没有权限dbo(我不希望它具有)。

如何将其传输到目标数据库上的新模式?

+0

您有两种选择:为schema_user1提供所需的权限,或者创建一个新用户来运行该工具,并为* *用户提供所需的权限。你现在要求的就是说:“我想让这个人有能力通过这个锁着的门,但不想给他们钥匙。” – 2014-10-17 14:12:56

+0

即使我授予schema_user1权限(或者使用具有所有权限的用户),数据库也会转移到“dbo”,我想知道是否可以将其转移到新的模式。 – 2014-10-20 05:52:47

+0

当然。在这种情况下,指定希望模式作为新用户的默认模式。 – 2014-10-20 12:09:28

回答

2

我必须指定Options.SchemaQualify = false作为Transfer对象(它指定是否必须在脚本中使用该架构)。

然后在新数据库中使用该用户,它将在新模式中创建。

相关问题