Author – Dave Cook
This is first in a series of blogs relating to managing the development of SQL Server Databases in Visual Studio.
You can manage your development of SQL Server Databases using Visual Studio Database Project Templates, these templates have been around for quite a number of years but it’s amazing how many developers are unaware of them or tried implementing them.
They can aid your development in a number of ways, including but not limited to:
- Convenient File Structure within the project
- Supports all SQL Object Types
- Objects are stored within individual files as TSQL
- Pre & Post Deployment Scripts
- Supports SQL CMD, SQL CLR & TSQL
- Syntax & Dependency Checking
- Intelisense
- Easy to manage in Source Control
- Full and Incremental Publishing(deployment) to Local, Remote & Cloud servers or platforms
- Database Compare – Compare a Project to a deployed Database
- Cross-Database queries using SQL CMD Variables
Within the first part of this series I will demonstrate how to create a Database Project by importing an existing database. Projects can be created from scratch, but I find that importing an existing database makes the whole process a lot more intuitive as the project file structure will be created for you.
The completed project that is created in this demo can be found in my public DevOps repository
Demo Database
For this walk through I will need access to a deployed database, I have created a database as per the database schema below:
Create the Database Project
Using Visual Studio create a new project by searching for and selecting the ‘SQL Server Database Project’ template.
When prompted provide a project name and path followed by clicking ‘Create’. This will produce an empty project structure.
Right click the Project and select ‘Import’ then ‘Database’ (As you can see you also have the options to import from a SQL Script or a DACPAC)
You will then be presented with the ‘Import Database’ dialog.
- Choose your database to import by clicking ‘Select Connection’, The Connect dialog will appear.
- Supply connection details to a local or remote(including cloud) database (Here I supply the connection server, credentials and the Demo database).
- Note that you can specify a different target project and optionally add to source control, however in this instance we will assume the current solution has already been configured for source control. You can also specify additional import settings e.g. add logins, permissions and existing database settings. We will leave this at the default as all other settings can be set once the import process is complete if required.
- Click ‘Start’
- All of the database objects will now be imported into the project and a folder and file structure created.
- Opening the files will display the editors as shown below:
Table Editor
Stored Procedure Editor
Editing the Database
Your database is now imported into a file based system. To add a new object simply right click the relevant folder and select ‘Add «Object Type»’
While editing your new object you will notice that you get intelisense and syntax checking.
Building the project will highlight any syntax or dependency errors. You are not able to publish (deploy) the database while there are build errors.
Publishing the Database
Before attempting to publish the database ensure that the correct ‘Target platform’ is set. As you can see, at time of writing the platform options can range from SQL Server 2005 through to Azure SQL Database. This option is available via the Project ‘Properties’ which can be selected by right clicking on the project.
Note: Ideally the ‘Target platform’ should be set as soon as the project is created, this will provide the correct intelisense for the target platform and avoid any potential build errors if unsupported commands have been set.

If you need to configure and Database level settings this can be done by clicking the ‘Database Settings…’ button which is located at the bottom right of the ‘Project Settings’ tab within Project ‘Properties’. Most database level settings can be set on any of the 3 tabs within ‘Database Settings’.

To publish or deploy the project right click the project and select ‘Publish’. You will notice that you can also access the ‘Schema Compare’ tool from the same context menu. This is an extremely useful tool that enables you to see the exact differences between your project (or other database) and any target database schemas, with the options to merge differences if required.
After selecting ‘Publish’, you will be presented with the ‘Publish Database’ dialog. Here you enter your target database connection and name for the target database. Selecting ‘Advanced’ will allow you to specify the rules around Full or Incremental deployment and how to handle scenarios where there may be a loss of potential data etc.
There are 2 main options here, ‘Publish’ which will deploy the database onto the target system or ‘Generate Script’ which will generate a deployment script without performing a deployment. If you simply want a dacpac, this is generated automatically after a build and can be found in the project bin folder.
Conclusion
Thank you for reading this blog. Hopefully it demonstrates how you can effectively manage your database development within your solution. It provides a great way to keep all of your code in a single place, simply by adding your service or application projects all into the same solution. And as an added bonus you get complete history of any changes via source control integration.Coming Next
My next article in this series will explain the benefits of adding Pre & Post Deployment Scripts and will move on to provide examples of how to create and use SQL CMD variables which amongst other things are necessary for cross database queries.
This article originally appeared at https://davecook.uk/blog/article/managing-sql-server-databases-in-visual-studio











