使用 Visual Studio 建立 Linux 的 SQL Server 資料庫

適用於:SQL Server - Linux

SQL Server Data Tools (SSDT) 可將 Visual Studio 轉化為適用於 Linux SQL Server 的強大開發和資料庫生命週期管理 (DLM) 環境。 您可以從原始檔控制的專案中來開發、建置、測試及發佈資料庫。 如同開發應用程式程式碼一樣。

安裝 Visual Studio 和 SQL Server Data Tools

  1. 如果您尚未在 Windows 電腦上安裝 Visual Studio,請下載並安裝 Visual Studio。 如果沒有 Visual Studio 授權,則 Visual Studio Community 版本是功能完整的 IDE,供學生、開放原始碼和個人開發人員免費使用。

  2. 在 Visual Studio 安裝期間,請針對 [選擇安裝類型] 選項選取 [自訂]。 選取 [下一步]

  3. 從功能選取清單中,選取 [Microsoft SQL Server Data Tools]、[Git for Windows] 和 [Visual Studio 的 GitHub 延伸模組]。

    Screenshot of SSDT setup.

  4. 繼續並完成安裝 Visual Studio。 這可能需要幾分鐘的時間。

取得最新版的 SQL Server Data Tools

SSDT 17.0 或更新版本支援 Linux 上的 SQL Server。

建立由原始檔控制的新資料庫專案

  1. 啟動 Visual Studio。

  2. 在 [檢視] 功能表上,選取 Team Explorer

  3. 在 [連線] 頁面上,選取 [本機 Git 存放庫] 區段中的 [新增]。

    Screenshot of the Local Git Repository section with the New option called out.

  4. 選取 [建立]。 建立本機 Git 存放庫之後,請按兩下 SSDTRepo

  5. 選取 [解決方案] 區段中的 [新增]。 在 [新增專案] 對話方塊中,選取 [其他語言] 節點下的 [SQL Server]

    Screenshot of the Solutions section with the New option and SQL Server option called out.

  6. 針對名稱輸入 TutorialDB,然後選取 [確定] 以建立新的資料庫專案。

在資料庫專案中,建立新的資料表

  1. 在 [檢視] 功能表上,選取 [方案總管]

  2. 以滑鼠右鍵按一下 [方案總管] 中的 TutorialDB,以開啟資料庫專案功能表。

  3. 選取 [新增] 底下的 [資料表]

    Screenshot showing how to create a new table using Add > Table.

  4. 使用資料表設計工具,新增「名稱 nvarchar(50)」和「位置 nvarchar(50)」這兩個資料行,如下圖所示。 當您在設計工具中新增資料行時,SSDT 會產生 CREATE TABLE 指令碼。

    Screenshot of the table designer with the Name and Location values called out.

  5. 儲存 Table1.sql 檔案。

建置和驗證資料庫

  1. 開啟 TutorialDB 上的資料庫專案功能表,然後選取 [建置]。 SSDT 會編譯專案中的 .sql 原始程式碼檔案,並建置資料層應用程式套件 (dacpac) 檔案。 這可用來將資料庫發佈至 Linux 上的 SQL Server 執行個體。

    Screenshot showing the TutorialDB with the Build option called out.

  2. 在 Visual Studio 中,查看 [輸出] 視窗的建置成功訊息。

將資料庫發佈至 Linux 上的 SQL Server 執行個體

  1. 開啟 TutorialDB 上的資料庫專案功能表,然後選取 [發佈]

  2. 選取 [編輯] 以選取 Linux 上的 SQL Server 執行個體。

    Screenshot showing the Publish option with the Edit option called out

  3. 在連線對話方塊中,鍵入 Linux 上 SQL Server 執行個體的 IP 位址或主機名稱、使用者名稱和密碼。

    Screenshot showing the connection dialog.

  4. 選取發佈對話方塊上的 [發佈] 按鈕。

  5. 查看 [資料工具作業] 視窗中的發佈狀態。

  6. 選取 [檢視結果] 或 [檢視指令碼],以查看 Linux 上的 SQL Server 上資料庫發佈結果的詳細資料。

    Screenshot showing the publish result with View Script and View Result called out.

您已成功在 Linux 上的 SQL Server 執行個體建立新資料庫,並了解使用原始檔所控制資料庫專案來開發資料庫的基本概念。