SQL Server中的数据层应用程序简介

In this article, I’m going to introduce the data-tier applications in SQL Server. As the official documentation from Microsoft says – “Data-tier applications in SQL Server are a logical entity that can be used to develop and manage most of the SQL Server objects like tables, views, stored procedures, functions etc. as a self-contained package“. Essentially, what that means is it is a component of SQL Server, using which we can develop, build, test and deploy databases for SQL Server just like we can do for any other web or desktop applications.

在本文中,我将介绍SQL Server中的数据层应用程序。 正如Microsoft的官方文档所述–“ SQL Server中的数据层应用程序是一个逻辑实体,可以作为独立的设备用于开发和管理大多数SQL Server对象,例如表,视图,存储过程,函数等。包装 ”。 从本质上讲,这意味着它是SQL Server的组件,使用它我们可以像为其他任何Web或桌面应用程序一样为SQL Server开发,构建,测试和部署数据库。

Data-tier applications in SQL Server were initially released around a decade back with SQL Server 2008 R2. The build of a data-tier application is a DACPAC file – Data-tier AppliCation PACkage, also known as a DAC file in short. A DAC is just another compressed zip folder that contains the model of the database that is being designed in XML format along with some other files necessary for the deployment. Using a DAC, developers can develop DDL scripts for each object in the database and these scripts will be built into a DAC file, which can later be used as a deployment utility to publish the databases to various SQL Server environments. This approach of deploying SQL Server databases has reduced the hassle of managing multiple migration scripts which in other cases the developers need to maintain for each version.

SQL Server中的数据层应用程序最初是在十年前与SQL Server 2008 R2一起发布的。 一个数据层应用程序的生成是一个DACPAC文件- 数据层 PPLIÇ通货膨胀PAC卡格,也被称为在一个短DAC文件。 DAC只是另一个压缩的zip文件夹,其中包含以XML格式设计的数据库模型以及部署所需的一些其他文件。 使用DAC,开发人员可以为数据库中的每个对象开发DDL脚本,并且这些脚本将内置到DAC文件中,该文件以后可用作部署实用程序,以将数据库发布到各种SQL Server环境。 这种部署SQL Server数据库的方法减少了管理多个迁移脚本的麻烦,在其他情况下,开发人员需要为每个版本维护。

在SQL Server Management Studio(SSMS)中创建数据层应用程序 (Create data-tier applications in SQL Server Management Studio (SSMS))

Developers and Database Administrators (DBAs) can easily create DACs using SQL Server Management Studio. This enables the users to extract the DAC file from an existing database and then perform other essential operations supported by the data-tier applications. You can follow the steps below in order to extract a data-tier application.

开发人员和数据库管理员(DBA)可以使用SQL Server Management Studio轻松创建DAC。 这使用户能够从现有数据库中提取DAC文件,然后执行数据层应用程序支持的其他基本操作。 您可以按照以下步骤操作,以提取数据层应用程序。

Right-click on the existing database for which you would like to create a data-tier application. Select Tasks and then select Extract Data-tier Application...

右键单击要为其创建数据层应用程序的现有数据库。 选择“ 任务” ,然后选择“ 提取数据层应用程序”

Extract Data Tier Applications in SQL Server

Figure 1 – Extract Data-tier Application in SQL Server Management Studio

图1 –在SQL Server Management Studio中提取数据层应用程序

The Extract Data-Tier Application page appears. Since this wizard will generate a DAC file, we will need to provide an application name which is basically the name of the DAC file that is going to be created and an optional description along with it.

出现“提取数据层应用程序”页面。 由于此向导将生成一个DAC文件,因此我们需要提供一个应用程序名称,该名称基本上是将要创建的DAC文件的名称以及一个可选的描述

Another important point to note here is that while extracting the data-tier applications in SQL Server, we can assign a version number for the DAC file. This helps us to maintain a version history of all the DAC files that we are going to create later. Finally, specify the location where are you going to create the DAC file. In this case, I’m going to store the DAC file under this directory – “C:\temp\WideWorldImportersDW.dacpac“. Click on Next once done.

这里要注意的另一个重要点是,在SQL Server中提取数据层应用程序时,我们可以为DAC文件分配版本号 。 这有助于我们维护以后要创建的所有DAC文件的版本历史记录。 最后,指定要在其中创建DAC文件的位置。 在这种情况下,我将DAC文件存储在此目录下-“ C:\ temp \ WideWorldImportersDW.dacpac ”。 完成后单击“ 下一步”

Extract Data Tier Application Wizard

Figure 2 – Extract Data-tier Application Wizard

图2 –提取数据层应用程序向导

Click on Next in the following pages and finally click on Finish. The data-tier application will be generated in the path that you have specified earlier.

接下来的页面中单击下一步 ,最后单击完成 。 数据层应用程序将在您之前指定的路径中生成。

Building the Data Tier Application

Figure 3 – Building the Data-tier Application

图3 –构建数据层应用程序

Now, that the data-tier application has been created, let’s go ahead and see the contents within the DAC file.

现在,已经创建了数据层应用程序,让我们继续看一下DAC文件中的内容。

DAC File Generated

Figure 4 – DAC File Generated

图4 –生成的DAC文件

The DAC file is a simple compressed zip archive which can be opened by any zip extractor. Rename the extension of the DAC file from DACPAC to ZIP and extract the contents to the folder.

DAC文件是一个简单的压缩zip存档,可以由任何zip提取程序打开。 将DAC文件的扩展名从DACPAC重命名为ZIP ,并将内容提取到该文件夹​​中。

Extract ZIP Archive

Figure 5 – Extract ZIP Archive

图5 –解压缩ZIP存档

Once you extract the contents of the DACPAC file, you’ll see there are four different XML files within it.

提取DACPAC文件的内容后,您将看到其中包含四个不同的XML文件。

DACPAC Contents

Figure 6 – DACPAC Contents

图6 – DACPAC目录

These XML files do not contain any data from the database. The database is scripted out only with the model information. In order to also export data, there is another file which the data-tier application supports, known as BACPAC. This is essentially, a backup along with the DACPAC file that is generated.

这些XML文件不包含来自数据库的任何数据。 仅使用模型信息来编写数据库脚本。 为了也导出数据,数据层应用程序支持另一个文件,称为BACPAC。 本质上,这是一个备份以及生成的DACPAC文件。

在SQL Server Management Studio中创建BACPAC文件 (Creating a BACPAC file in SQL Server Management Studio)

The steps to create a BACPAC file is almost similar to that of creating a DACPAC. Instead of choosing Extract Data-Tier Application, in this case, you need to select the Export Data-tier Application.

创建BACPAC文件的步骤几乎与创建DACPAC的步骤相似。 在这种情况下,您无需选择“ 提取数据层应用程序” ,而是选择“ 导出数据层应用程序”

Export Data Tier Applications in SQL Server Management Studio

Figure 7 – Export Data-tier Application in SQL Server Management Studio

图7 –在SQL Server Management Studio中导出数据层应用程序

Proceed to the Export Settings page by clicking on Next. On this page, you can see there are two tabs – Settings and Advanced. Here, you can specify where would you like the BACPAC file to be created. You also have an option on this page to save the BACPAC file directly to Microsoft Azure instead. In this case, I’m going to save it on my local directory itself.

单击“ 下一步 进入“ 导出设置”页面。 在此页面上,您可以看到两个选项卡- 设置高级 。 在这里,您可以指定要在何处创建BACPAC文件。 您也可以在此页面上选择将BACPAC文件直接保存到Microsoft Azure。 在这种情况下,我将其保存在本地目录中。

Export Data Tier Application Settings

Figure 8 – Export Data-tier Application Settings

图8 –导出数据层应用程序设置

On the same page, there’s also a second tab – Advanced. This allows us to choose from a list table under all the schemas available in the database that we are trying to export. You can choose the tables for which the data needs to be generated in the BACPAC file. Let’s select all the tables for this example and click on Next and finally click on Finish.

在同一页面上,还有另一个选项卡– Advanced 。 这使我们可以从列表表中选择要导出的数据库中所有可用模式下的列表。 您可以在BACPAC文件中选择需要为其生成数据的表。 让我们选择该示例的所有表,然后单击Next ,最后单击Finish

Export Data Tier Application Advanced Setting

Figure 9 – Export Data-tier Application Advanced Setting

图9 –导出数据层应用程序高级设置

The operation might take a while based on the size of the data that is being exported. You can see a list of all the tables that have been exported in the BACPAC file. Once the operation is completed successfully, click Close.

该操作可能需要一些时间,具体取决于要导出的数据的大小。 您可以在BACPAC文件中看到所有已导出表的列表。 操作成功完成后,点击关闭

Export BACPAC Operation

Figure 10 – Export BACPAC Operation

图10 –导出BACPAC操作

Like the DACPAC file, the BACPAC file is also a compressed zip archive which can be renamed into a ZIP extension and we can view the contents within. I have renamed the extension of the file to zip and extracted the contents.

像DACPAC文件一样,BACPAC文件也是压缩的zip存档,可以将其重命名为ZIP扩展名,我们可以查看其中的内容。 我已将文件扩展名重命名为zip并提取了内容。

BACPAC File Contents

Figure 11 – BACPAC File Contents

图11 – BACPAC文件内容

As you can see in the figure above, in addition to the contents of the DACPAC file, we have two new directories in this – “_rels” and “Data“. In the Data directory, you can see a directory has been created for all the tables that you’ve selected while exporting the BACPAC file.

如上图所示,除了DACPAC文件的内容之外,我们在其中还有两个新目录-_relsData 。 在“ 数据”目录中,您可以看到在导出BACPAC文件时为所有选择的表创建了目录。

BACPAC Data Directories

Figure 12 – BACPAC Data Directories

图12 – BACPAC数据目录

Also, if you open further, each of these data directories contains a BCP file for that table, which is essentially the BCP utility in the SQL Server to bulk copy these data back into the database when restored.

另外,如果进一步打开,则这些数据目录中的每个目录都包含该表的BCP文件,该文件实质上是SQL Server中的BCP实用程序,用于在还原时将这些数据批量复制回数据库中。

BACPAC File Contents

Figure 13 – BACPAC File Contents

图13 – BACPAC文件内容

在SQL Server Management Studio中导入数据层应用程序 (Importing a data-tier application in SQL Server Management Studio)

We can import the BACPAC file back to the SQL Server instance, and the database will be restored as-is on the target server. Since we can import a BACPAC file, then the data along with the schema will be restored on the server. You can follow the steps below to import a data-tier application in SQL Server.

我们可以将BACPAC文件导入回SQL Server实例,数据库将在目标服务器上按原样还原。 由于我们可以导入BACPAC文件,因此数据和模式将在服务器上恢复。 您可以按照以下步骤在SQL Server中导入数据层应用程序。

Right-click on the Databases and select “Import Data-Tier Application…” from the context menu.

右键单击数据库,然后从上下文菜单中选择“ Import Data-Tier Application… ”。

Import Data Tier Applications in SQL Server

Figure 14 – Import Data-tier Applications in SQL Server

图14 –在SQL Server中导入数据层应用程序

On the next page that appears, provide the proper path of the BACPAC file and click on Next.

在出现的下一页上,提供BACPAC文件的正确路径 ,然后单击“ 下一步”

BACPAC File Location

Figure 15 – BACPAC File Location

图15 – BACPAC文件位置

In the Database Settings page that appears, you can choose the name of the database and the location where the data files should be created. By default, it will take the path of the SQL Server Default Data directory. Click Next and finally on Finish once done.

在出现的“数据库设置”页面中,您可以选择数据库的名称以及应该在其中创建数据文件的位置。 默认情况下,它将采用SQL Server默认数据目录的路径。 一旦完成后,单击下一步 ,终于完成

Database Settings

Figure 16 – Database Settings

图16 –数据库设置

The wizard will import the data-tier application and a database will be created once the process is completed successfully.

该向导将导入数据层应用程序,一旦成功完成该过程,便会创建一个数据库。

Import Completed for Data Tier Applications in SQL Server

Figure 17 – Import Completed

图17 –导入完成

结论 (Conclusion)

In this article, I have explained what data-tier applications in SQL Server are. I have also explained how to create a DACPAC and BACPAC using SQL Server Management Studio. DACPAC files are just a copy of the schema from the entire database without any data whereas, in the BACPAC file, data is included as well. In my upcoming articles for data-tier applications in SQL Server, I’ll explain how to start developing the same from scratch using Visual Studio and SQL Server Data Tools.

在本文中,我解释了SQL Server中的数据层应用程序。 我还介绍了如何使用SQL Server Management Studio创建DACPAC和BACPAC。 DACPAC文件只是整个数据库中模式的副本,没有任何数据,而在BACPAC文件中,数据也包括在内。 在我即将发表的有关SQL Server中数据层应用程序的文章中,我将说明如何使用Visual Studio和SQL Server数据工具从头开始开发相同的内容。

翻译自: https://www.sqlshack.com/an-introduction-to-data-tier-applications-in-sql-server/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值