azure云数据库_使用Azure SQL数据库构建ASP.NET应用

azure云数据库

In this article, you will learn about Azure SQL Database and its uses. Then the article splits into two sets of tutorials. The first part will show you how to create a single database in Azure SQL Database. The second tutorial will show you how to build an ASP.NET app in Azure SQL Database.

在本文中,您将了解Azure SQL数据库及其用法。 然后,本文分为两套教程。 第一部分将向您展示如何在Azure SQL数据库中创建一个数据库。 第二个教程将向您展示如何在Azure SQL数据库中构建ASP.NET应用程序。

ASP.NET is an open-source server-side framework, which was developed by Microsoft. It has evolved into a feature-rich development platform, and it is typically used for building dynamic websites, services, and applications. Azure is Microsoft’s cloud computing service, so it is only natural to use it for ASP.NET apps.

ASP.NET是Microsoft开发的开放源代码服务器端框架。 它已经发展成为功能丰富的开发平台,通常用于构建动态网站,服务和应用程序。 Azure是Microsoft的云计算服务,因此将它用于ASP.NET应用程序是很自然的。

Illustrated computer screens displaying abstract depiction of code

什么是Azure SQL数据库? ( What is Azure SQL Database? )

Azure SQL Database is a fully-managed database service offered by Azure. It enables you to easily deploy databases in the cloud, on-premises, or hybrid environments. The service is powered by Microsoft SQL Server engines that provide many of the same features as a standard SQL Server. These include:

Azure SQL数据库是Azure提供的完全托管的数据库服务。 它使您可以轻松地在云,本地或混合环境中部署数据库。 该服务由Microsoft SQL Server引擎提供动力,这些引擎提供了许多与标准SQL Server相同的功能。 这些包括:

  • Language features – enables you to control flow language keywords and supports custom variables, stored procedures, sequence numbers, predicates, DML statements, cursors, and data types 语言功能 –使您可以控制流语言关键字,并支持自定义变量,存储过程,序列号,谓词,DML语句,游标和数据类型
  • Database features – including automatic tuning (plan forcing), data compression, online index operations, partitioning, temporal tables, change tracking, database collation, and contained users/databases 数据库功能 –包括自动调整(计划强制),数据压缩,在线索引操作,分区,临时表,更改跟踪,数据库整理以及所包含的用户/数据库
  • Security features – including support for application roles, threat detection, row-level security, and dynamic data masking 安全功能 –包括对应用程序角色,威胁检测,行级安全和动态数据屏蔽的支持
  • Multi-model capabilities-supports JSON, OPENJSON, XML, OPENXML, Spatial, and graph processing 多模型功能 -支持JSON,OPENJSON,XML,OPENXML,空间和图形处理

Benefits of Azure SQL Database include:

Azure SQL数据库的优点包括:

  • Automated backups with long-term retention for archiving

    具有长期保留功能的自动备份以进行归档
  • Built-in geo-replication of data for data loss prevention and disaster recovery

    内置数据的地理复制以防止数据丢失和灾难恢复
  • migration of databases from on-premises or other services 迁移数据库
  • Easy optimization of resources with automatic performance tuning

    通过自动性能调整轻松优化资源
  • High-availability resources for mission-critical data

    关键任务数据的高可用性资源
  • Scalability to minimize costs and meet workload demands

    可扩展性以最小化成本并满足工作负载需求

Use cases of Azure SQL Database include:

Azure SQL数据库的用例包括:

  • Backup and restore – you can use cloud databases as a backup for on-premise databases. When synchronized, cloud databases can also serve as failovers if needed 备份和还原 –您可以将云数据库用作内部部署数据库的备份。 同步后,如果需要,云数据库也可以用作故障转移
  • Development and testing environments – cloud applications can be easily connected to environments for development and testing. Once you are ready to deploy an application, you can create a production environment quickly using a database copy 开发和测试环境 –云应用程序可以轻松连接到开发和测试环境。 一旦准备好部署应用程序,就可以使用数据库副本快速创建生产环境。
  • Business continuity – Geo-replication and high availability features ensure that mission-critical workloads remain available and ensure that data is not lost in a disaster or attack 业务连续性 –地理复制和高可用性功能可确保关键任务工作负载保持可用,并确保在灾难或攻击中数据不会丢失
  • Analytics – Azure SQL Database integrates with Azure analytics services, including Data Lake Store and Data Warehouse. This integration enables you to easily reference database data, stage that data, and perform data transformations all in one place 分析 – Azure SQL数据库与Azure分析服务集成在一起,包括Data Lake Store和Data Warehouse。 通过这种集成,您可以轻松地在一个地方引用数据库数据,暂存该数据并执行数据转换。

在Azure SQL数据库中创建一个数据库 (Creating a single database in Azure SQL Database)

Before you begin connected apps to your databases, you may want to get familiar with the Azure SQL Database service. The following walkthrough guides you through the steps needed to create a single database from the Azure portal. These steps are summarized-see the full tutorial in the Azure documentation.

在开始将应用程序连接到数据库之前,您可能需要熟悉Azure SQL数据库服务。 以下演练指导您完成从Azure门户创建单个数据库所需的步骤。 这些步骤已汇总-请参阅Azure文档中的完整教程。

创建数据库 (Create the database)

  • SQL databases present in the vertical menu on the left-hand side SQL数据库
  • Click the Create SQL database button to create a SQL Database on Azure

    单击创建SQL数据库按钮 在Azure上创建SQL数据库

    Creating a new Azure SQL Database from the Azure Portal

选择项目和数据库设置 (Select project and database settings)

  • In the Project details section, on the Basics tab, configure the following:

    在“ 项目详细信息”部分, 在“ 基本”选项卡上,配置以下各项:

    • Subscription: click the drop-down and select the appropriate subscription

      订阅:单击下拉列表并选择适当的订阅
    • Create new, enter your resource group name, and click 新建” ,输入资源组名称,然后单击“ OK 确定”。


  • In the Database etails section, configure the following:

    在“ 数据库详细信息”部分中,配置以下内容:

    • Database name: enter a unique name

      数据库名称:输入唯一名称
    • Server: select Create new, configure the following values, and then click OK

      服务器:选择“ 新建” ,配置以下值,然后单击“ 确定”。

      • Server name: choose a unique ID for your database server

        服务器名称:为数据库服务器选择唯一的ID
      • Server admin login: choose an admin username for your database

        服务器管理员登录:为数据库选择管理员用户名
      • Password: choose a secure password you will use to access your database

        密码:选择用于访问数据库的安全密码
      • Location: choose the location in which you want to run the database

        位置:选择要在其中运行数据库的位置


    • Compute + storage: select Configure database and choose your preferred deployment option (Serverless), Data max size, and vCores. You can learn more about the options here
    • 计算+存储:选择配置数据库,然后选择首选的部署选项( Serverless数据最大大小和vCore。 您可以在此处了解更多有关选项的信息


  • Select Apply

    选择应用

    Configuring your new Azure SQL Database server

选择高级设置 (Select advanced settings)

  • In the Networking tab, determine if you want to create a private endpoint or allow external access to the database

    在“ 网络”选项卡中,确定是要创建私有端点还是要允许外部访问数据库

    • Add current client IP address is set to 添加当前客户端IP地址”设置为“ Yes 是”


  • In the Additional Settings tab, under Data source, choose if you want to create a blank database, or import the data from a backup

    在“ 其他设置”选项卡的“ 数据源”下 ,选择是要创建空白数据库还是要从备份导入数据

    • There is a sample database you can use under the existing database option if you prefer

      如果您愿意,可以在现有数据库选项下使用一个示例数据库


  • Create 创建

使用SQL数据库在Azure中构建ASP.NET应用 (Build an ASP.NET app in Azure with SQL Database)

Once you’re comfortable with how creating a database works, you should be ready to attach databases to your applications. When hosting applications in Azure, an easy option is to use the Azure App Service. This service enables you to deploy applications in a highly scalable and self-patching environment.

一旦对创建数据库的工作方式感到满意之后,就应该准备将数据库附加到应用程序。 在Azure中托管应用程序时,一个简单的选择是使用Azure App Service。 该服务使您可以在高度可扩展的自修补环境中部署应用程序。

One of the types of applications you can create is an ASP.NET application. ASP.NET is a server-side framework that you can use to create dynamic web sites, services and applications.

可以创建的应用程序类型之一是ASP.NET应用程序。 ASP.NET是一个服务器端框架,可用于创建动态网站,服务和应用程序。

In the following tutorial, you’ll be shown how to deploy application code to App Service and connect your application to an Azure SQL Database instance. These steps are summarized-see the full tutorial in the Azure documentation.

在下面的教程中,将向您展示如何将应用程序代码部署到App Service并将应用程序连接到Azure SQL数据库实例。 这些步骤已汇总-请参阅Azure文档中的完整教程。

先决条件 (Prerequisites)

To follow this tutorial, you need to have Visual Studio (VS) 2019 installed along with the ASP.NET and web development workload. You can find Visual Studio here and the workload in VS under Tools and Get Tools and Features.

要遵循本教程,您需要安装Visual Studio(VS)2019以及ASP.NET和Web开发工作负载。 您可以在此处找到Visual Studio 并在VS中的“工具”和“获取工具和功能”下找到工作负载。

If you already have an ASP.NET application that you wish to use, feel free to do so. If not, you can use this sample project which is a basic MVC create-read-update-delete app based on Entity Framework Code First.

如果您已经拥有要使用的ASP.NET应用程序,请随时使用。 如果没有,则可以使用此示例项目 ,该示例项目是基于Entity Framework Code First的基本MVC create-read-update-delete应用程序。

If you are using the sample project, you need to open the project file and run the app without debugging. Once open, create a few to-do items so that you have tasks to work with. This should generate a connection string in the Models/MyDatabaseContext.cs file that you need to reference once you’re ready to connect to your database.

如果使用的是示例项目,则需要打开项目文件并运行应用程序而无需调试。 打开后,创建一些待办事项,以便您可以处理任务。 一旦准备好连接到数据库,这将在Models / MyDatabaseContext.cs文件中生成一个连接字符串,您需要引用该字符串。

将您的应用发布到Azure (Publish your app to Azure)

  • In Solution Explorer, right-click your project and select Publish

    在解决方案资源管理器中, 右键单击您的项目,然后选择“ 发布”。

    • Make sure that you are publishing to Azure App Service

      确保您要发布到Azure App Service

      Publishing your Azure App via Solution Explorer in Visual Studio

  • Go to the Create App Service dialog, and then choose to Add an account

    转到“创建应用程序服务”对话框,然后选择“ 添加帐户”

    • If you already have an Azure subscription, sign in to it

      如果您已经有Azure订阅,请登录
    • If not, you can use the free trial to demo the service

      如果没有,您可以使用免费试用版来演示服务

      Configuring Web App settings for application connected to Azure SQL Database instance

  • A web application name will be automatically generated for you. You can either keep this name or select another unique name

    Web应用程序名称将自动为您生成。 您可以保留此名称或选择其他唯一名称

    • Keep in mind, whichever name you choose will be part of your app URL

      请记住,您选择的任何名称都会成为您应用网址的一部分
    • This name cannot be shared with any other Azure app, including those of other users

      此名称不能与任何其他Azure应用程序共享,包括其他用户的名称


  • Select a Resource Group for your application. If you do not have any groups set up, you can create a New one

    为您的应用程序选择一个资源组 。 如果您没有设置任何组,则可以创建一个新组

    • Resource groups are containers used to group your applications, databases, and storage accounts. These groups make it easier to manage various resources

      资源组是用于对应用程序,数据库和存储帐户进行分组的容器。 这些组使管理各种资源更加容易


  • Select an existing App Service plan or create a New one

    选择一个现有的App Service计划或创建一个新的

    • Location and 位置Size. Once defined, click 大小。 定义后,单击“ OK 确定”。
    • How you configure your plan determines your app region, instance sizes, scale, and SKU

      配置方案的方式决定了您的应用程序区域,实例大小,规模和SKU


创建一个SQL Server实例 (Create a SQL Server instance)

This tutorial assumes that you want to create a new database instance to use with your application. If you prefer to connect an existing database you can do so in the Application Settings of your application. Choose Connection Strings and copy the string collected earlier into the string field.

本教程假定您要创建一个新的数据库实例以与您的应用程序一起使用。 如果您希望连接现有数据库,则可以在应用程序的“应用程序设置”中进行连接。 选择连接字符串,然后将先前收集的字符串复制到字符串字段中。

To create a new database, you must first have an Azure SQL Database logical server. If you do not have one created, you can do so by clicking Create a SQL Database under the Explore additional Azure services section.

若要创建新数据库,必须首先具有Azure SQL数据库逻辑服务器。 如果尚未创建,则可以通过单击“浏览其他Azure服务”部分下的“ 创建SQL数据库”来进行。

  • New 新建”。
  • In the pop-up dialog, configure your details as follows and click OK

    在弹出对话框中,如下配置您的详细信息,然后单击“ 确定”。

    • Server Name: you can use the generated name or create your own. The name must be a unique name across all Azure instances. This name will be part of the URL of your server

      服务器名称:您可以使用生成的名称或创建自己的名称。 该名称在所有Azure实例中必须是唯一的名称。 该名称将成为服务器URL的一部分
    • Administrator Username and Administrator Password

      管理员用户名和管理员密码

      • Take note of these as you need these credentials to manage your instance

        请注意这些,因为您需要这些凭据来管理您的实例


  • When returned to the Configure SQL Database pop-up, configure your:

    返回到“配置SQL数据库”弹出窗口时,配置以下各项:

    • Database Name: you can leave this as the auto-generated name or choose a unique name

      数据库名称:您可以将其保留为自动生成的名称,也可以选择一个唯一的名称
    • Connection String Name: enter the connection string you obtained earlier from your Models/MyDatabaseContext.cs file

      连接字符串名称:输入您先前从Models / MyDatabaseContext.cs文件获得的连接字符串
    • You can also do this through the App Services section of the portal

      您也可以通过门户的“应用程序服务”部分执行此操作

      Configure Azure SQL Database Connection string through App Services in portal

  • OK 确定
  • Verify that your configurations are correct and select Create

    确认您的配置正确,然后选择创建

    • If you need to change configurations, you can do so by returning to the previous screens and changing settings as needed

      如果需要更改配置,可以通过返回到先前的屏幕并根据需要更改设置来进行。

结论 (Conclusion)

Azure SQL Database is a powerful service that provides many useful features. Amongst the most useful features are the built-in backup and the geo-replication model. These two can help you keep your data safe and available at any time. However, the most important aspect is that both Azure SQL Database and ASP.NET are maintained by Microsoft. This means the integration is seamless and allows you to focus on building.

Azure SQL数据库是一项功能强大的服务,提供许多有用的功能。 内置备份和地理复制模型是最有用的功能。 这两个可以帮助您随时保护数据安全和可用。 但是,最重要的方面是Microsoft维护Azure SQL数据库和ASP.NET。 这意味着集成是无缝的,使您可以专注于构建。

翻译自: https://www.sqlshack.com/build-an-asp-net-app-with-azure-sql-database/

azure云数据库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值