Friday, December 2, 2022

DataBases changes using Dacpac

 Objective

This Documentation will show to work the database used in database created, patterns used and how to connect to them in the three environments Dev, test and UAT.

 Tools

·        SGBD: SQL server

·        Cloud: Azure SQL Server

·        Designer: SQL Project (visual Studio)

·        view/Edit Data: Azure Data Studio / SSMS

Table structure

        CREATE TABLE [dbo].[Student]
        (
[Sno] [int] NOT NULL,
[Student_ID] Int NOT NULL,
[Student_name] [varchar](50) NOT NULL,
[Date_of_Birth] [datetime] NOT NULL,
[Weight] [int] NULL
        );

 As in the example above, every table must have an ID of type integer, which is the primary key of the Entity, this key must be automatically generated (identity).

Environments and how to connect with them

Azure data studio/ SQL server is used to the database and view and edit the data. the table below present the database

environment and URLs

Environment

SQL server

Database

dev

dev-001.databse.window.net

dev

How to connect

To connect the respective Environment, you have two options to connect one is through azure data studio, SSMS and server Explorer.

Through Azure Data Studio


Through SSMS


Through Server Explorer


How to Add/Edit

How to create/Edit Table 

To create a table, right click on the table folder → Add → Table. it will show two ways to edit the table, visually in table format, or in script format.

To edit a table, double click the corresponding script int the table section, it will show two ways to edit the table, visually in table format, or in script format. As shown below:


It is recommended to pay attention to the script, as it is where validations and revisions are carried out by the developers. Most important is to build your project, it will not give any error. Otherwise, the pipeline will fail.

How to create/Edit Store Procedure

To create a Store Procedure, right click on the Store Procedures folder → Add → Store Procedure. It will show in script format. You can write a script.

To edit a table, double click the corresponding script int the table section, it will show two ways to edit the table, visually in table format, or in script format. As shown below:

It is recommended to pay attention to the script, as it is where validations and revisions are carried out by the developers. Most important is to build your project, it will not give any error. Otherwise, the pipeline will fail.

To test the Procedure that it is giving the desired result, you can connect to the Predev Environment by using above mentioned detail to How to connect the database. After testing if everything looks fine you can push your changed in the featured branch.

How to create/Edit Functions

To create a Function, right click on the Function folder → Add → Function. It will show in script format. You can write a script.

To edit a table, double click the corresponding script int the table section, it will show in script format. As shown below:

It is recommended to pay attention to the script, as it is where validations and revisions are carried out by the developers. Most important is to build your project, it will not give any error. Otherwise, the pipeline will fail.

To test the Procedure that it is giving the desired result, you can connect to the Predev Environment by using above mentioned detail to How to connect the database. After testing if everything looks fine you can push your changed in the featured branch.

How to create/Edit Views

To create a Store Procedure, right click on the View folder → Add → View. It will show in script format. You can write a script.

To edit a view, double click the corresponding script in the view section, it will show in script format. As shown below:

It is recommended to pay attention to the script, as it is where validations and revisions are carried out by the developers. Most important is to build your project, it will not give any error. Otherwise, the pipeline will fail.

To test the Procedure that it is giving the desired result, you can connect to the Predev Environment by using above mentioned detail to How to connect the database. After testing if everything looks fine you can push your changed in the featured branch.

How to create/Edit Pre and Post Development Scripts

To create a Pre/Post development script right click on the Script folder → Add → Scripts. Select your desired script. It will show in script format. You can write a script.

To edit a Pre/Post development script, double click the corresponding script in the Script section, it will show in script format. As shown below:

It is recommended to pay attention to the script, as it is where validations and revisions are carried out by the developers. Most important is to build your project, it will not give any error. Otherwise, the pipeline will fail.

To test the Procedure that it is giving the desired result, you can connect to the Predev Environment by using above mentioned detail to How to connect the database. After testing if everything looks fine you can push your changed in the featured branch.

Push your Code

After making the changes in your feature branch, move up your feature branch and request a pull request to your feature branch. After team has reviewed it, and the pull request has been approved, it should be published to respective environment in azure.

How the pipeline will work is as shown below












Wednesday, November 30, 2022

Creating .dacpac file from SQL Server and creating Database Project With Visual Studio

Creating .dacpac file from SQL Server and creating Database Project With Visual Studio

In this article, I am going to explain about SQL Server Database Project available in Visual Studio. I will cover the following points,

  • What is .dacpac File
  • Creating the .dacpac file
  • Introduction of SQL Server Database Project.
  • Create New SQL Server Database Project.
  • Importing the .DACPAC file in Database Project.

Prerequisites

 Visual Studio and MS SQL Server/ Azure data studio.

 

What is Dacpac File  

A file with .dacpac which stands for for Data Tier AppliCation Package. This extension is a database file, created with Microsoft SQL Server data tier application, that contains the database model for representation of database objects. As it contains the complete model of the database, it is used to restore a database from the details available in the model. This will help inn database import and export operations. 

In simple word we can say data-tier application package in the form of window file containing all the data structure in a single unit.

What is the Purpose of the DACPAC file.

DACPAC helps the DBA and the developer to package   into a single unit . it will we handed over to the team member for deploying the database to the target environments in a manual ( hitting the target environment through visual studio / VS code) and automated manner (through azure pipeline or from GitHub pipeline).

How to create DACPAC file 

There are many options to create the DACPAC file but explaining 2 options from that :

  • Use SSMS
  • Use Visual Studio
  • Azure data studio

Creating DACPAC using SSMS

SQL Server Management Studio is a easy tools through which you can generate DACPAC.

Open SSMS, right-click on database (in Object Explorer) and go to menu: Tasks -> Extract Data-tier Application 
For your reference adding the pictures also :-



Creating DACPAC from visual studio
it is the most best solution for creating the DACPAC file from Visual Studio we can change some configuration and create the dacpac file. Suppose we have some error in the database . some old Store procedure needs to be deleted using table old columns which was deleted recently . similarly Function, Views are using old column which was deleted recently. For those Scenario's we are taking the help of visual studio. 
By disabling that option you will avoid the issue and it doesn’t matter whether the database contains errors or not – the DAC package file will be generated.
Open extract window by using menu: View -> SQL Server Object Explorer, select required database and right-click on it and choice Extract Data-tier Application…


Creating DACPAC using Azure Data Studio
To create the DACPAC file from the azure data studio. Right-click the Databases folder or right-click a specific database in the Object Explorer. Then, select Data-tier Application Wizard



To create a new project and import existing database schema

To create a new project and import existing database schema

Click File àNew-à Project -à New Project dialog box à select SQL Server à SQL Server Database Project à Name the DataBase Project -àclick ok.

 


There is no specific platform for this project in previous versions of Visual Studio. You will be able to set your target platform in the Project Settings dialog box after the project has been created.

To Import the .DACPAC file into newly created Database

Right-click on Solution Explorer à Import à Database.



The Import Database dialog box opens. In the Source database connection section, click Choose a DACPAC file. The Import Database dialog shows a progress bar and displays a list of objects SSDT is importing. When the import operation has completed, click Finish to exit the final screen.


 

Examine the hierarchy in the Solution Explorer. Expand the dbo folder and you will find separate Functions, Tables and Views folders. Notice that the tables and function are grouped under their schema folders.



Now you can create any Tables, Store procedure , Function , View , Pre- development , Post -development scripts .. etc. from this database Project . After that push the code in the respective repository and push your changes in SQL database.
  


next Blog
https://ashissharma85.blogspot.com/2022/12/databases-changes-using-dacpac.html