使用SMO复制数据库,尝试覆盖源数据库

时间:2011-08-24 09:14:49

标签: c# sql-server smo

我尝试在SQL Server 2008 R2中复制数据库。我想将数据库用作模板,然后在服务器中以其他名称(其他所有内容应该相同)复制它。

我使用我在网上找到的代码,但总是说,它无法创建source.mdf,因为它已经存在。

    // Connect to Server
    Server server = new Server(txtSourceServer.Text);

    // Get the Database to Transfer
    Database db = server.Databases[txtSource.Text];

    // Setup transfer
    // I want to copy all objects
    // both Data and Schema
    Transfer t = new Transfer(db);
    t.CopyAllObjects = true;
    t.DropDestinationObjectsFirst = true;
    t.CopySchema = true;
    t.CopyData = true;
    t.DestinationServer = txtDestServer.Text;
    t.DestinationDatabase = txtDest.Text;
    t.Options.IncludeIfNotExists = true;
    t.CreateTargetDatabase = true;

    // Transfer Schema and Data
    t.TransferData();

    // Kill It
    server = null;

这是正确的方法还是有其他方法?使用Oracle我会使用导出和导入但是使用SQL Server我只是丢失了:(

0 个答案:

没有答案