使用命令行导出 SQL Server 数据层应用程序
點擊上方藍字關注“汪宇杰博客”
我們可以使用 SSMS 導出 SQL Server 數據庫的數據層應用程序。我在本地機器上使用這種方法已經有好幾年了。如果不知道什么是 DAC,您可以參考 Microsoft 文檔:
https://docs.microsoft.com/en-us/sql/relational-databases/data-tier-applications/data-tier-applications?view=sql-server-2017
數據層應用程序 (DAC) 是一個邏輯數據庫管理實體,用于定義與用戶數據庫關聯的所有 SQL Server 對象,如表、視圖和實例對象(包括登錄名)。DAC 是 SQL Server 數據庫部署的一個自包含單元,它使數據層開發人員和數據庫管理員能夠將 SQL Server 對象打包到一個名為“DAC 包”(也稱作 DACPAC)的可移植項目中。
但是,要跑在自動化環境下工作, 如 CI/CD 環境,或一些自動的計劃任務。我們通常需要在命令行下完成。
微軟提供了一個跨平臺的命令行工具, 可以導入/導出DAC: sqlpackage.exe
它是用.NET寫的,目前 Linux 和 macOS 的版本還在預覽中。對于 Windows,可以在此處下載并安裝:https://go.microsoft.com/fwlink/?linkid=2087429
這個工具會被安裝到?C:\Program Files\Microsoft SQL Server\150\DAC\bin 下面,但是它不會自動更改PATH環境變量,因此要使用這個工具得手工切換到安裝目錄。
要導出數據層應用程序,需要指定3個參數:
/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 文件導出成功
總結
以上是生活随笔為你收集整理的使用命令行导出 SQL Server 数据层应用程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET Core模块化前后端分离
- 下一篇: 6月数据库排行:PostgreSQL和M