c# - 如何使用 SMO 将数据库传输到新模式?

标签 c# sql-server smo

我想使用 Microsoft.SqlServer.Management.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 权限(我不希望它有)。

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

最佳答案

我必须为 Transfer 对象指定 Options.SchemaQualify = false(指定是否必须在脚本中使用模式)。

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

关于c# - 如何使用 SMO 将数据库传输到新模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26420133/

相关文章:

c# - 静态关键字正确使用?

sql-server - 如何从sql server中的字符串插入日期时间

sql-server - 什么时候会释放 "nested transaction"中的 XLOCK?

c# - SMO 相当于 SET HADR OFF

SQL Server : Why do use SMO?

c# - WPF/XAML 绑定(bind) : Work with real DataContext

c# - 带有字形信息的字体渲染

c# - 恢复数据库时表单无响应

c# - 如何按特定顺序运行多个事件?

java.sql.SQLException : Network error IOException: failed to connect ETIMEDOUT 异常