Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. I would like to have this data in the Sql Server Project, so when I publish it, it publishes the data as well.

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.

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. 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.

You can created either from SSMS: 1. DACPAK: Select your database, right click for Tasks-> Extract Data Tier App. 2. Select database, right click for Tasks-> Export Data Tier App.

like image 245
Greg Gum Avatar asked Nov 06 '13 18:11

Greg Gum


2 Answers

Years later, I finally figure this out: There are two types of exports:

  1. DACPAC - which includes only the structure
  2. BACPAC which includes data and structure.

You can create either from SSMS:

  1. DACPAC: Select your database, right click for Tasks-> Extract Data Tier App.
  2. BACPAC: Select database, right click for Tasks-> Export Data Tier App.
like image 188
Greg Gum Avatar answered Sep 23 '22 14:09

Greg Gum


This isn't exactly from Visual Studio as you asked but it might be a workaround. If your db exists in a dev server then maybe you can use SqlPackage.exe Export and Extract actions:

For Export

By default, data for all tables will be included in the .bacpac file.

A SqlPackage.exe Export action exports a live database from SQL Server or Windows Azure SQL Database to a BACPAC package (.bacpac file). By default, data for all tables will be included in the .bacpac file. 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.

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

For Extract

Makes a DACPAC with the user table data

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:

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. The upgrade wizard presents presents checkbox options to execute the pre- and/or post-deployment scripts in the .dacpac file. 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. This requires the DACPAC author to ship the pre-/post-deployment scripts alongside the DACPAC file. 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).

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. (Exampe: "msbuild /Target:Deploy DeploymentDemo.dbproj").

like image 34
Heinrich Avatar answered Sep 20 '22 14:09

Heinrich