使用命令行导出 SQL Server 数据层应用程序
點(diǎn)擊上方藍(lán)字關(guān)注“汪宇杰博客”
我們可以使用 SSMS 導(dǎo)出 SQL Server 數(shù)據(jù)庫(kù)的數(shù)據(jù)層應(yīng)用程序。我在本地機(jī)器上使用這種方法已經(jīng)有好幾年了。如果不知道什么是 DAC,您可以參考 Microsoft 文檔:
https://docs.microsoft.com/en-us/sql/relational-databases/data-tier-applications/data-tier-applications?view=sql-server-2017
數(shù)據(jù)層應(yīng)用程序 (DAC) 是一個(gè)邏輯數(shù)據(jù)庫(kù)管理實(shí)體,用于定義與用戶數(shù)據(jù)庫(kù)關(guān)聯(lián)的所有 SQL Server 對(duì)象,如表、視圖和實(shí)例對(duì)象(包括登錄名)。DAC 是 SQL Server 數(shù)據(jù)庫(kù)部署的一個(gè)自包含單元,它使數(shù)據(jù)層開(kāi)發(fā)人員和數(shù)據(jù)庫(kù)管理員能夠?qū)?SQL Server 對(duì)象打包到一個(gè)名為“DAC 包”(也稱作 DACPAC)的可移植項(xiàng)目中。
但是,要跑在自動(dòng)化環(huán)境下工作, 如 CI/CD 環(huán)境,或一些自動(dòng)的計(jì)劃任務(wù)。我們通常需要在命令行下完成。
微軟提供了一個(gè)跨平臺(tái)的命令行工具, 可以導(dǎo)入/導(dǎo)出DAC: sqlpackage.exe
它是用.NET寫(xiě)的,目前 Linux 和 macOS 的版本還在預(yù)覽中。對(duì)于 Windows,可以在此處下載并安裝:https://go.microsoft.com/fwlink/?linkid=2087429
這個(gè)工具會(huì)被安裝到?C:\Program Files\Microsoft SQL Server\150\DAC\bin 下面,但是它不會(huì)自動(dòng)更改PATH環(huán)境變量,因此要使用這個(gè)工具得手工切換到安裝目錄。
要導(dǎo)出數(shù)據(jù)層應(yīng)用程序,需要指定3個(gè)參數(shù):
/Action:{Extract|DeployReport|DriftReport|Publish|Script|Export|Import} Specifies the action to be performed. (short form /a)
/SourceConnectionString:<string>Specifies a valid SQL Server/Azure connection string to the source database. If this parameter is specified it shall be used exclusively of all other source parameters. (short form /scs)
/TargetFile:<string> Specifies a target file (i.e., a .dacpac files) to be used as the target of action instead of a database. If this parameter is used, no other target parameter shall be valid. This parameter
shall be invalid for actions that only support database targets. (short form /tf)
例如
SqlPackage.exe /a:export /scs:"Server=(local);Database=moonglade-dev;Trusted_Connection=True;" /tf:"D:\moonglade-dev-20190520.bacpac"
可以看到 bacpac 文件導(dǎo)出成功
總結(jié)
以上是生活随笔為你收集整理的使用命令行导出 SQL Server 数据层应用程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ASP.NET Core模块化前后端分离
- 下一篇: 所以Apache基金会不受美国法律约束?