简体   繁体   English

如何在 DacPac 中包含数据

[英]How to include data in a DacPac

In Sql Server Data Tools for Visual Studio, you can create a Sql Server Project and import the structure of a Database.在 Visual Studio 的 Sql Server Data Tools 中,您可以创建一个 Sql Server 项目并导入数据库的结构。 This works very well.这非常有效。

Is it also possible to import data as well?是否也可以导入数据? For example, lets say I have a Type table with several types.例如,假设我有一个包含多种类型的 Type 表。 I would like to have this data in the Sql Server Project, so when I publish it, it publishes the data as well.我想在 Sql Server 项目中有这些数据,所以当我发布它时,它也会发布数据。

The closest I have been able to figure out is to use Sql Server Object Explorer to create a script for the data and then manually add that script to the project.我能想到的最接近的方法是使用 Sql Server 对象资源管理器为数据创建一个脚本,然后手动将该脚本添加到项目中。

When I saw the demo of Sql Server Data Tools, then showed publishing a project, but then using copy and paste to get data into the database.当我看到Sql Server Data Tools 的demo 时,然后显示发布一个项目,然后使用复制和粘贴来获取数据到数据库中。 Surely there is a better way.当然还有更好的方法。

EDIT Years later, I finally figure this out: There are two types of exports: 1. DACPAK - which includes only the structure, 2. The BACBAK which includes data and structure.编辑多年后,我终于弄清楚了:有两种类型的导出:1. DACPAK - 仅包含结构,2. 包含数据和结构的 BACBAK。

You can created either from SSMS: 1. DACPAK: Select your database, right click for Tasks-> Extract Data Tier App.您可以从 SSMS 创建: 1. DACPAK:选择您的数据库,右键单击 Tasks-> Extract Data Tier App。 2. Select database, right click for Tasks-> Export Data Tier App. 2. 选择数据库,右键单击任务-> 导出数据层应用程序。

This isn't exactly from Visual Studio as you asked but it might be a workaround.正如您所问的,这并不完全来自 Visual Studio,但这可能是一种解决方法。 If your db exists in a dev server then maybe you can use SqlPackage.exe Export and Extract actions:如果您的数据库存在于开发服务器中,那么您可以使用SqlPackage.exe ExportExtract操作:

For Export出口

By default, data for all tables will be included in the .bacpac file.默认情况下,所有表的数据都将包含在 .bacpac 文件中。

A SqlPackage.exe Export action exports a live database from SQL Server or Windows Azure SQL Database to a BACPAC package (.bacpac file). SqlPackage.exe 导出操作将实时数据库从 SQL Server 或 Windows Azure SQL 数据库导出到 BACPAC 包(.bacpac 文件)。 By default, data for all tables will be included in the .bacpac file.默认情况下,所有表的数据都将包含在 .bacpac 文件中。 Optionally, you can specify only a subset of tables for which to export data.或者,您可以仅指定要导出数据的表的子集。 Validation for the Export action ensures Windows Azure SQL Database compatibility for the complete targeted database even if a subset of tables is specified for the export.即使为导出指定了表的子集,导出操作的验证也可确保 Windows Azure SQL 数据库与完整目标数据库的兼容性。

sqlpackage.exe /action:Export /TargetFile:"test.bacpac" 
    /sourceDatabasename:test 
    /sourceservername:.\testserver

You can import your bacpac file https://msdn.microsoft.com/en-us/library/hh710052.aspx您可以导入您的bacpac文件https://msdn.microsoft.com/en-us/library/hh710052.aspx

For Extract提取物

Makes a DACPAC with the user table data使用用户表数据创建 DACPAC

sqlpackage.exe /action:Extract /TargetFile:"test.dacpac" 
    /sourceDatabasename:test 
    /sourceservername:".\testserver"
    /p:ExtractAllTableData=true

If you don't want to use SqlPackage.exe , this article is old but it has three workarounds that might work:如果您不想使用 SqlPackage.exe这篇文章很旧,但它有三个可能有效的解决方法:

1) Redeploy the same .dacpac file using SSMS's "Upgrade Data-tier application..." wizard under the [Server]/Management/Data-tier Application/[Application Name] node in the SSMS Object Explorer. 1) 在 SSMS 对象资源管理器中的 [Server]/Management/Data-tier Application/[Application Name] 节点下,使用 SSMS 的“升级数据层应用程序...”向导重新部署相同的 .dacpac 文件。 The upgrade wizard presents presents checkbox options to execute the pre- and/or post-deployment scripts in the .dacpac file.升级向导会显示复选框选项以执行 .dacpac 文件中的部署前和/或部署后脚本。 Enabling both the pre- and post-deployment script options, then performing the upgrade will produce the expected result.启用部署前和部署后脚本选项,然后执行升级将产生预期结果。

2) Manually execute the DACPAC pre- and/or post-deployment T-SQL script files using a SSMS query window, SQLCMD.exe, or simliar. 2) 使用 SSMS 查询窗口、SQLCMD.exe 或类似工具手动执行 DACPAC 部署前和/或部署后 T-SQL 脚本文件。 This requires the DACPAC author to ship the pre-/post-deployment scripts alongside the DACPAC file.这要求 DACPAC 作者将部署前/部署后脚本与 DACPAC 文件一起发送。 Alternatively, the pre-/post-deployment scripts can be extracted using the Microsoft DacUnpack.exe utility, or a ZIP file utility (after renaming the file extension from .dacpac to .zip).或者,可以使用 Microsoft DacUnpack.exe 实用程序或 ZIP 文件实用程序(将文件扩展名从 .dacpac 重命名为 .zip 后)提取部署前/部署后脚本。

3) Use either MSBuild.exe (v4.0.30319.1, or higher) or Visual Studio 2010 Premium SP1 (or higher) to deploy the "SQL Server Data-tier Application" project file. 3) 使用 MSBuild.exe(v4.0.30319.1 或更高版本)或 Visual Studio 2010 Premium SP1(或更高版本)部署“SQL Server 数据层应用程序”项目文件。 (Exampe: "msbuild /Target:Deploy DeploymentDemo.dbproj"). (例如:“msbuild /Target:Deploy DeploymentDemo.dbproj”)。

Years later, I finally figure this out: There are two types of exports:多年后,我终于弄明白了:有两种类型的出口:

  1. DACPAC - which includes only the structure DACPAC - 仅包含结构
  2. BACPAC which includes data and structure. BACPAC 其中包括数据和结构。

You can create either from SSMS:您可以从 SSMS 创建:

  1. DACPAC: Select your database, right click for Tasks-> Extract Data Tier App. DACPAC:选择您的数据库,右键单击任务-> 提取数据层应用程序。
  2. BACPAC: Select database, right click for Tasks-> Export Data Tier App. BACPAC:选择数据库,右键单击任务-> 导出数据层应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM