以编程方式使用SSDT导入数据库 [英] Import database with SSDT programmatically

查看:125
本文介绍了以编程方式使用SSDT导入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,需要从数据库中复制所有内容(数据除外),然后将其部署到新数据库中。我目前正在通过从现有数据库导入SSDT,然后将项目发布到新数据库中来进行此操作。

I have a situation where I need to copy everything out of a database (except data) and deploy it to a new database. I am currently doing this by importing from the existing database into SSDT and then publish the project to the new database.

我想知道是否可以通过编程方式进行此操作。理想情况下,我希望有一个过程,可以为该过程指定要从中复制数据库的名称,要复制到的数据库名称,然后该过程将自动执行导入和导出。

I want to know if there is a way I can do this programmatically. Ideally I would like to have a process in place where I can give the process the name of the database to copy from, the name of the database to copy to, and then the process would just automatically carry out the import and export.

执行此操作的好方法是什么?

What might be a good way to do this?

推荐答案

SSDT是由称为数据层应用程序框架(也称为DacFX)的库提供支持。 DacFX是一个公共API,可用于提取和发布dacpac文件。您可以在类似以下目录的Visual Studio或SQL Server下的程序文件中找到DacFX的副本:

SSDT is powered by a library called the Data-Tier Application Framework (also known as DacFX). DacFX is a public API that you can use to extract and publish dacpac files. You can find a copy of DacFX in Program Files under Visual Studio or SQL Server in a directory something like this:


  • C:\Program文件(x86)\ Microsoft Visual Studio 14.0\Common7\IDE\扩展名\Microsoft\SQLDB\DAC\130

  • C:\Program Files(x86 )\Microsoft SQL Server\130\DAC\bin

您可以在此处下载DacFX的最新版本:
https://www.microsoft.com/en-us/download/details。 aspx?id = 51672

You can download the latest version of DacFX here: https://www.microsoft.com/en-us/download/details.aspx?id=51672

请注意,在安装DacFX时,还必须安装其依赖项SqlSysClrTypes和SqlDom,这些都可以在系统要求中找到。

Note that when you install DacFX it's also necessary to install its dependencies, SqlSysClrTypes and SqlDom, which can be found in the System Requirements section of the above download page.

要使用DacFX提取并发布dacpac文件,可以使用SqlPackage.exe ,如下所示:

To use DacFX to extract and publish a dacpac file, you can use SqlPackage.exe, like so:

C :\程序文件(x86)\Microsoft SQL Server\130\DAC\bin\SqlPackage.exe / a:提取/ scs:数据源= YOURSERVER;初始目录= YOURDB;集成安全性= true / tf:C:\temp\yourdb .dacpac

C:\Program Files(x86)\Microsoft SQL Server\130\DAC \bin\SqlPackage.exe / a:发布/ tcs:数据源= YOURSERVER;初始目录= YOUROTHERDB;集成安全性= true /sf:C:\temp\yourdb.dacpac

或者,您可以通过Microsoft.SqlServer.Dac API以编程方式使用DacFX,如下所示:
using Microsoft.SqlServer。 Dac;
类程序
{
静态void Main(string [] args)
{
DacServices ds = new DacServices( Data Source = YOURSERVER; Initial Catalog = YOURDB;集成安全性= true);
ds.Extract(@ C:\temp\yourdb.dacpac, YOURDB, AppName,新的System.Version());
使用(DacPackage dp = DacPackage.Load(@ C:\temp\yourdb.dacpac))
{
ds.Deploy(dp, YOUROTHERDB);
}
}
}

Alternately, you can use DacFX programmatically using the Microsoft.SqlServer.Dac API, like so: using Microsoft.SqlServer.Dac; class Program { static void Main(string[] args) { DacServices ds = new DacServices("Data Source=YOURSERVER;Initial Catalog=YOURDB;Integrated Security=true"); ds.Extract(@"C:\temp\yourdb.dacpac", "YOURDB", "AppName", new System.Version()); using (DacPackage dp = DacPackage.Load(@"C:\temp\yourdb.dacpac")) { ds.Deploy(dp, "YOUROTHERDB"); } } }

这篇关于以编程方式使用SSDT导入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆