SQL 数据库应用程序开发概述SQL Database application development overview

本文逐步讲解开发人员在编写代码以连接到 Azure SQL 数据库时应考虑的基本注意事项。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 数据库的所有部署模型(单一数据库、弹性池、托管实例)。This article applies to all deployment models of Azure SQL Database (Single database, Elastic pools, Managed instance).

提示

如果需要设置 Azure SQL 数据库,请查看单一数据库托管实例的入门指南。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 数据库。You can use various programming languages and platforms to connect and query Azure SQL Database. 可以找到可用于连接到 Azure SQL 数据库的示例应用程序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 数据库可与 Microsoft 工具(如 Visual StudioSQL Server Management Studio)配合使用。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.

身份验证Authentication

对 Azure SQL 数据库的访问使用登录名和防火墙进行保护。Access to Azure SQL Database is protected with logins and firewalls. Azure SQL 数据库支持 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.

连接Connections

在客户端连接逻辑中,将默认超时替换为 30 秒。In your client connection logic, override the default timeout to be 30 seconds. 默认值 15 秒对于依赖于 Internet 的连接而言太短。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.

复原能力Resiliency

Azure SQL 数据库是一种云服务,在其中可能会遇到在底层基础结构中或云实体之间的通信中发生的暂时性错误。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 数据库在发生暂时性基础结构故障时可复原,但这些故障可能会影响连接。Although Azure SQL Database is resilient on the transitive infrastructure failures, these failures might affect your connectivity. 如果在连接到 SQL 数据库时发生暂时性错误,代码应重试调用When a transient error occurs while connecting to SQL Database, your code should retry the call. 建议让重试逻辑使用退让逻辑,这样就不会因为多个客户端同时重试而对 SQL 数据库造成混乱。We recommend that retry logic use backoff logic, so that it does not overwhelm the SQL Database with multiple clients retrying simultaneously. 重试逻辑取决于 SQL 数据库客户端程序的错误消息Retry logic depends on the error messages for SQL Database client programs.

有关如何为 Azure SQL 数据库上的计划内维护事件做好准备的详细信息,请参阅规划 Azure SQL 数据库中的 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 数据库的所有功能Explore all the capabilities of SQL Database.