您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

应用程序开发概述 - SQL 数据库和 SQL 托管实例Application development overview - SQL Database & SQL Managed Instance

适用于: Azure SQL 数据库 Azure Synapse Analytics

本文逐步讲解开发人员在编写代码以连接到 Azure 数据库时应考虑的基本注意事项。This article walks through the basic considerations that a developer should be aware of when writing code to connect to your database in Azure. 本文适用于 Azure SQL 数据库和 Azure SQL 托管实例。This article applies to Azure SQL Database, and Azure SQL Managed Instance.

语言和平台Language and platform

可以使用各种编程语言和平台连接和查询 Azure SQL 数据库。You can use various programming languages and platforms to connect and query Azure SQL Database. 可以找到可用于连接到数据库的示例应用程序You can find sample applications that you can use to connect to the 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 身份验证用户和登录名。Azure SQL Database supports both SQL Server and Azure Active Directory authentication users and logins. Azure Active Directory 登录名仅在 SQL 托管实例中可用。Azure Active Directory logins are available only in SQL 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. 建议让重试逻辑使用退让逻辑,这样就不会因为多个客户端同时重试而对该服务造成混乱。We recommend that retry logic use backoff logic, so that it does not overwhelm the service 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 数据库SQL 托管实例的所有功能。Explore all the capabilities of SQL Database and SQL Managed Instance.

开始之前,请参阅 Azure SQL 数据库Azure SQL 托管实例的相关指南信息。To get started, see the guides for Azure SQL Database and Azure SQL Managed Instances.