共用方式為


快速入門:產生程式碼

在這份快速入門中,您將學習 GitHub Copilot 如何透過直接在 Visual Studio Code 中產生情境感知程式碼,加速 SQL 與物件關聯映射(ORM)的開發。 GitHub Copilot 幫助你架構資料表、演化結構結構,並減少重複的腳本。 無論你使用 T-SQL,還是使用 Entity Framework、Sequelize、Prisma 或 SQLAlchemy 等 ORM,GitHub Copilot 都能幫助你專注於建立應用程式邏輯。

開始

請確定您已連線到資料庫,並使用 MSSQL 擴充功能開啟使用中的編輯器視窗。 此連線可讓 @mssql 聊天參與者瞭解您的資料庫環境內容,提供精確且切合內容的建議。 如果沒有資料庫連線,聊天參與者就不會有架構或數據內容來提供有意義的回應。

下列範例使用 AdventureWorksLT2022 範例資料庫,您可以從 Microsoft SQL Server 範例和社群專案 首頁下載。

為了獲得最佳結果,請調整數據表和架構名稱以符合您自己的環境。

請確保聊天中包含@mssql前綴。 例如,輸入 @mssql ,後面接著您的問題或提示。 這可確保聊天參與者瞭解您要求與 SQL 相關的協助。

使用 GitHub Copilot 進行程式碼產生

使用 GitHub Copilot 來產生 SQL 和 ORM 相容的程式代碼,以反映連線的資料庫結構,並遵循最佳做法。 從定義數據表和關聯性到腳本檢視、建置移轉檔案,或建置數據存取層和 API,GitHub Copilot 可協助您更快且更有信心地移動。

以下是您可以透過聊天參與者詢問的常見使用案例和範例:

產生 SQL 程式碼

GitHub Copilot 能協助你為多種開發情境產生 SQL 程式碼,從腳本撰寫、建立與修改資料表,到撰寫儲存程序與檢視。 這些範例說明如何使用 GitHub Copilot 將重複的 SQL 腳本自動化,並遵循 T-SQL 開發的最佳做法。

把所有資料表都寫成結構

Script out all the tables in the `SalesLT` schema as `CREATE TABLE` statements in SQL.

使用儲存程序擷取客戶資料

Write a SQL stored procedure in my current database. The procedure should retrieve all customers from the `SalesLT.Customer` table where the `LastName` matches a given parameter. Make sure to use T-SQL best practices.

寫出一個包含所有限制和索引的表格

Script out the `SalesLT.Customer` table as a `CREATE TABLE` statement, including all constraints and indexes.

寫一個連接兩個資料表的視圖腳本

Generate a SQL script to create a view that joins the `SalesLT.Customer` and `SalesLT.SalesOrderHeader` tables, showing customer names and their total order amounts.

透過新增欄位來修改表格

Write a SQL script to alter the `SalesLT.Customer` table by adding a `last_updated` column with a default timestamp.

產生 ORM 遷移

GitHub Copilot 可以根據您選擇的架構內容和架構,產生 ORM 相容的移轉和模型定義。 從 Sequelize 到 Entity Framework、Prisma 和 SQLAlchemy,GitHub Copilot 可協助建立與應用程式數據模型一致的 Scaffold 變更。

產生一個模型來新增欄位

Generate a Sequelize (JavaScript) model to add an `email` column (`varchar(256)`) to the `SalesLT.Customer` table.

產生實體框架模型類別

Generate an Entity Framework model class in C# to represent a `SalesLT.ProductModel` table with `id`, `name`, and `description` columns.

產生實體框架模型

Generate an Entity Framework model in C# based on the existing `SalesLT.Product` table.

寫 Python 程式碼來定義一個表格

Write SQLAlchemy code to define a `SalesLT.OrderDetails` table with `id`, `order_date`, and `customer_id` fields, ensuring compatibility with `Python`.

寫一個參數化查詢

Using SQLAlchemy, write a parameterized query that retrieves all customers from the `SalesLT.Customer` table where the `LastName` matches a provided parameter.

更新現有模型以新增資料表

Update my existing Prisma model (schema.prisma) to define a new `SalesLT.Order` model with `id`, `customer_id`, and `order_date` fields.

為一個資料表產生一個模型類別

Generate a SQLAlchemy model class for the `SalesLT.Product` table, including columns and data types.

產生重複使用的應用程式程序代碼

GitHub Copilot 也可以幫助搭建與您的 SQL 資料庫互動的後端和前端元件。 這些範例示範如何使用 Azure Functions、Node.js、Django 和 Next.js等熱門堆棧,從架構移至運作中的應用程式程序代碼。

無伺服器後端 SQL 系結和 Blazor

以下範例展示了你可以在 GitHub Copilot Chat 中使用的完整提示,來搭建端對端解決方案。 這些提示包括詳細的指示和內容,可協助 Copilot 跨後端和前端層產生精確的結構化程序代碼。

Generate a full-stack app using Azure SQL bindings for Functions and Blazor WebAssembly. Follow these steps:

1. Backend: Azure Functions (C#) with SQL Bindings

   - Configure SQL Bindings to automatically read and write data from the `SalesLT.Customer` table.
   - Implement HTTP-triggered functions with the following endpoints:
     - `GET /api/customers` – Fetch all customers.
     - `GET /api/customers/{id}` – Get a specific customer by ID.
     - `POST /api/customers` – Create a new customer.
     - `PUT /api/customers/{id}` – Update an existing customer.
     - `DELETE /api/customers/{id}` – Delete a customer.
   - Use `Dependency Injection` for database connections and logging.
   - Include an `appsettings.json` file to store database connection strings and environment variables.
   - Use `Azure Functions Core Tools` to run and test the functions locally.

1. Frontend: Blazor WebAssembly (Optional)

   - Create a Blazor WebAssembly frontend that consumes the API.
   - Display a table with customer data and a form to add new customers.
   - Use `HttpClient` to call the Azure Functions endpoints.
   - Implement two-way data binding to handle form inputs dynamically.
   - Use Bootstrap or Blazor components for styling and layout.

Ensure the project includes setup instructions for running both the Azure Functions backend and Blazor WebAssembly frontend locally, with proper `.env` or `local.settings.json` configurations for database connections.

全端使用 Node.js 與 Next.js

以下範例是你可以在 GitHub Copilot Chat 中提供的詳細提示,用以產生完整的後端設定,包括 API 路由和資料庫整合。

Generate a REST API using Node.js with Express that connects to my local SQL Database. Use the Tedious package for SQL Server connections and Prisma as the ORM. Follow these steps:

1. Backend: Node.js + Express

   - Establish a database connection using Prisma with Tedious as the SQL Server driver.
   - Implement API routes for `SalesLT.Customer` with the following endpoints:
     - `GET /customers` – Fetch all customers.
     - `GET /customers/:id` – Get a specific customer by ID.
     - `POST /customers` – Create a new customer.
     - `PUT /customers/:id` – Update an existing customer.
     - `DELETE /customers/:id` – Delete a customer.
   - Configure `Prisma` to map the `SalesLT.Customer` table and generate database migrations using `prisma migrate dev`.
   - Use `dotenv` for environment variables (database credentials, ports, etc.).
   - Add `Jest` for testing the API endpoints.

1. Frontend: Next.js + TypeScript (Optional)

   - Create a Next.js frontend that consumes the API.
   - Display a table with customer data and a form to add new customers.
   - Use React hooks (`useState`, `useEffect`) to manage state and fetch data dynamically.
   - Style the UI using Tailwind CSS.
   - Implement server-side data fetching (`getServerSideProps`) in Next.js for improved performance.

Ensure the project includes setup instructions for running both the backend and frontend independently, with proper `.env` configurations for the database connection.

後端:Django + Django REST 架構

以下範例是你可以在 GitHub Copilot Chat 中提供的詳細提示,用以產生完整的後端設定,包括 API 路由和資料庫整合。

Scaffold a Django backend with Django REST Framework for the `SalesLT.Customer` table. Follow these steps:

- Implement API routes using Django's `ModelViewSet` with the following endpoints:
  - `GET /customers` – Fetch all customers.
  - `GET /customers/{id}` – Get a specific customer by ID.
  - `POST /customers` – Create a new customer.
  - `PUT /customers/{id}` – Update an existing customer.
  - `DELETE /customers/{id}` – Delete a customer.

- Add instructions for generating database migrations with `python manage.py makemigrations` and `migrate`.

分享您的體驗

若要協助我們精簡及改善 MSSQL 延伸模組的 GitHub Copilot,請使用下列 GitHub 問題範本來提交您的意見反應: GitHub Copilot 意見反應

提交意見反應時,請考慮包括:

  • 測試的案例 – 讓我們知道您專注於哪些領域,例如架構建立、查詢產生、安全性、當地語系化。

  • 表現良好之處 – 描述任何感覺順暢、有幫助或超乎您期望的體驗。

  • 問題或錯誤 – 包含任何問題、不一致或混淆的行為。 螢幕快照或螢幕錄製特別有用。

  • 改進建議 – 分享改善可用性、擴大涵蓋範圍或增強 GitHub Copilot 回應的想法。