SQL Database 應用程式開發概觀SQL Database application development overview

本文將逐步解說開發人員在撰寫程式碼以連接到 Azure SQL Database 時應注意的基本考量事項。This article walks through the basic considerations that a developer should be aware of when writing code to connect to Azure SQL Database. 本文適用於 Azure SQL Database (單一資料庫、彈性集區、受控執行個體) 的所有部署模型。This article applies to all deployment models of Azure SQL Database (Single database, Elastic pools, Managed instance).


若您需要設定 Azure SQL Database,請查看快速入門指南,以了解單一資料庫受控執行個體Look at the getting started guides for single databases and managed instances if you need to setup your Azure SQL Database.

語言和平台Language and platform

您可以使用各種程式設計語言與平台連線及查詢 Azure SQL Database。You can use various programming languages and platforms to connect and query Azure SQL Database. 您可以找到可用於連線至 Azure SQL Database 的範例應用程式You can find sample applications that you can use to connect to the Azure SQL Database.

您可以利用 cheetahsql-cliVS Code 等開放原始碼工具。You can leverage open-source tools like cheetah, sql-cli, VS Code. 此外,Azure SQL Database 使用 Visual StudioSQL Server Management Studio 等 Microsoft 工具。Additionally, Azure SQL Database works with Microsoft tools like Visual Studio and SQL Server Management Studio. 您也可以使用 Azure 入口網站、PowerShell 和 REST API 協助您獲得額外生產力。You can also use the Azure portal, PowerShell, and REST APIs help you gain additional productivity.


Azure SQL Database 的存取受到登入及防火牆的保護。Access to Azure SQL Database is protected with logins and firewalls. Azure SQL Database 支援 SQL Server 及 Azure Active Directory (AAD) 驗證 的使用者和登入。Azure SQL Database supports both SQL Server and Azure Active Directory (AAD) authentication users and logins. AAD 登入僅適用於受控執行個體。AAD logins are available only in Managed Instance.

深入了解管理資料庫存取和登入Learn more about managing database access and login.


在您的用戶端連線邏輯中,將預設的逾時覆寫為 30 秒。In your client connection logic, override the default timeout to be 30 seconds. 預設 15 秒對於依賴網際網路的連線而言太短。The default of 15 seconds is too short for connections that depend on the internet.

如果您使用的是 連接集區,請確定在程式未主動使用時即時關閉連接,而不是準備重複使用連接。If you are using a connection pool, be sure to close the connection the instant your program is not actively using it, and is not preparing to reuse it.

請避免長時間執行的交易,因為任何基礎結構或連線失敗可能會復原交易。Avoid long-running transactions because any infrastructure or connection failure might roll back the transaction. 可能的話,請將交易分割成多個較小的交易,並使用批次處理來改善效能If possible, split the transaction in the multiple smaller transactions and use batching to improve performance.


Azure SQL Database 為雲端服務,因此基礎結構或雲端實體之間的通訊可能會發生暫時性錯誤。Azure SQL Database is a cloud service where you might expect transient errors that happen in the underlying infrastructure or in the communication between cloud entities. 雖然 Azure SQL Database 會從可轉移的基礎結構失敗中復原,但這些失敗可能會影響您的連線。Although Azure SQL Database is resilient on the transitive infrastructure failures, these failures might affect your connectivity. 當連接到 SQL Database 發生暫時性錯誤時,您的程式碼應該重試呼叫When a transient error occurs while connecting to SQL Database, your code should retry the call. 我們建議重試邏輯使用輪詢邏輯,因此它不會同時重試多個用戶端而讓 SQL Database 超過負荷。We recommend that retry logic use backoff logic, so that it does not overwhelm the SQL Database with multiple clients retrying simultaneously. 重試邏輯取決於 SQL Database 用戶端程式的錯誤訊息Retry logic depends on the error messages for SQL Database client programs.

如需如何為 Azure SQL 資料庫上的計劃性維護事件進行準備的詳細資訊,請參閱規劃 Azure SQL Database 的 Azure 維護事件For more information about how to prepare for planned maintenance events on your Azure SQL database, see planning for Azure maintenance events in Azure SQL Database.

網路考量事項Network considerations

後續步驟Next steps

瀏覽 SQL Database 的所有功能Explore all the capabilities of SQL Database.