Using Database Projects for Visual Studio

by Ira 6. May 2008 19:31

Do you use database projects in Visual Studio? If not, then now is a good time to start. It is the best way I have found to source control my databases without actually sticking the database file itself in the repository. It is very simple to add to your solution and creates a default directory structure for your create scripts, change scripts and queries. Best of all the project will be recognized by source control and added to the source code repository. This means you can keep versioned scripts for maintainability. It also gives you the option to do the dreaded rollback!

 

Lets start by creating a database project of our own. To start, we will create a blank Visual Studio solution.

screenshot-2008-05-05_18_56_00

 

Now click File -> Add -> New Project.

screenshot-2008-05-05_18_57_08

 

You should get the dialog below. Now we will look in the node of "Other Project Types", click "Database" then select "Database Project". This will give us the opportunity to name our database project. We will call this one by the database we are going to be using, good ol' Northwind.

screenshot-2008-05-05_18_58_44

 

Next you will be asked to setup a database reference. So here all we have to do is add a new reference to the Northwind database.

screenshot-2008-05-05_18_59_29

 

My database will be in SQL Server, yours can be in any of the choices shown below.

screenshot-2008-05-05_19_01_31

 

Now we just set the server name and choose the Northwind database.

screenshot-2008-05-05_19_02_07

 

You should then test your connection (out of good practice) and see the dialog below upon success.

screenshot-2008-05-05_19_02_15

 

Now we will see our database reference shown in the original list of references. Just double click on your newly added reference.

screenshot-2008-05-05_19_02_31

 

Now your project should look something like the shot below.

screenshot-2008-05-05_19_02_51

 

Once we get this far, we need to start filling our folders with database scripts. You can add new folders if you wish, but I think the default folder structure is good enough. Now then, we can use the database publishing wizard to get our first create script into the "Create Scripts" folder.

 

Double click the reference to your database in the database references and it should bounce you to the server explorer window with your data connection all setup.

screenshot-2008-05-05_19_05_20

 

Right click your data connection and click "Publish to provider..." to start the database publishing wizard.

screenshot-2008-05-05_19_05_31 

 

Now we select the database we would like to publish, so again I will choose Northwind.

screenshot-2008-05-05_19_05_52

 

Now with the "Script to file" option chosen, we will browse to the "Create Scripts" folder in our database project to put the outputted .sql file.

screenshot-2008-05-05_19_06_51

 

Now we get to choose some options. I am going to set the output for SQL Server 2005 and select to script the database schema only.

screenshot-2008-05-05_19_07_18

 

Now we can review the summary of what we have configured and click finish.

screenshot-2008-05-05_19_07_26

 

Upon clicking finish, we will see the progress of the .sql file being scripted to the hard drive.

screenshot-2008-05-05_19_08_07

 

Ok, for some reason I couldn't find a refresh button in the database project to see the file that was created so we will right click the "Create Scripts" folder and click "Add Existing Item..."

screenshot-2008-05-05_19_10_15

 

Then browse to our .sql file and click the "Add" button.

screenshot-2008-05-05_19_10_27

 

Now we should see it in our project and we can also see the contents of the generated script by opening it in the editor.

screenshot-2008-05-05_19_12_20

screenshot-2008-05-05_19_12_31

 

It really is just that simple. I mean if nothing else you have a designated place to put all your SQL scripts for your database. The big plus is that your scripts will be source controlled. Also, anyone who opens the solution will know exactly which databases are used by the project.

 

Hope this helps!

 

kick it on DotNetKicks.com

Tags: , ,

Visual Studio | Database | SQL

Comments

5/7/2008 10:07:36 AM #

Kevin Berridge

I have tried using Visual Studio's database projects as well as Management Studio's database projects but have always been disappointed by both.

Your tutorial on how to set up the solution is great.  But I start having problems with it when it comes to keeping it up to date.  Here are some of the issues I've had, perhaps you'll have some insight.

Do you update the scripts in the project, or update the database objects directly?
Do you simply regenerate the scripts every time the database changes?
How do you handle "shared database objects" which are likely to be used in many different databases?

Kevin Berridge United States

5/7/2008 11:04:48 AM #

Ira

Well, I can only speak for myself but the answers to your questions are as follows:

Do you update the scripts in the project, or update the database objects directly?
I update the scripts in my database project and use those scripts to update my database. Then commit the scripts to version control.

Do you simply regenerate the scripts every time the database changes?
Not EVERY time, but in a timely fashion as the database evolves. You should also be using backup tools in SQL as well so you can recover without the scripts. However if you are using a deployable packaged project, your create scripts should be updated by the end of your changes.

How do you handle "shared database objects" which are likely to be used in many different databases?
Shared objects can get tricky. I add a reference to all databases that my project involves. I also create some documentation files in the database project outlining the shared objects and how they are used.

Hope this helps.

Ira United States

5/7/2008 12:39:45 PM #

Jeff Garoutte

It is worth noting that you can also right click on a table, view, stored proedure or function and select "Generate Create Script to Project" from the server explorer in visual studio once you have a database project in the solution.

The script will be named <database>.<owner>.<objectname>.sql in the create scripts folder (for example Northwind.dbo.Employess.sql).

You can select multipule objects and it will generate them in a file called script<number>,sql.

I often generate a script for each object, this allows me to edit it without wading in 10,000 lines of SQL looking for a view that I need to add a column too.  It also gives me a clear view of what changed and when in source control.

It takes longer to step up an exisiting database this way, but when starting a new database there is not much difference in speed.

Often I find myself creating a "generate all" script for an exisitng database and the smaller "one object" file as needed.

The fact that you can change the projects database reference or right click a script and select "run on" makes setting up a "temp" testing database a breeze

Jeff Garoutte United States

5/7/2008 4:42:37 PM #

Will Asrari

<3 DB projects

@Ira: Right-click a stored procedure > 'Run On' > Select instance of SQL.

Will Asrari United States

5/7/2008 6:07:36 PM #

Ira

@Will: Thanks for the tip!

Ira United States

5/7/2008 8:27:00 PM #

VB

Better use Microsoft SQL Server Management Studio. It has the same level of Source Control support but gives you much more tools to work with your database. There is also a very handy script - "Script table data" in the CodeSmith. I use it very often when I need to script lookup table data.

VB United States

5/8/2008 3:22:00 AM #

Simone

Did you ever try rikmigration or any other "rake:db" style DB versioning tool?

Simone Italy

5/8/2008 8:10:11 AM #

Ira

Did you ever try rikmigration or any other "rake:db" style DB versioning tool?
No, I have only tried the tools available to me within Visual Studio and Management Studio. I will have a look into it though.

Ira United States

5/8/2008 9:20:44 AM #

pingback

Pingback from alvinashcraft.com

Dew Drop - May 8, 2008 | Alvin Ashcraft's Morning Dew

alvinashcraft.com

7/2/2008 3:32:15 AM #

Mattias Andersson

I have spent some time reading all posts and I still don't see how I get a changed script or stored procedure in the project to get stored in the actual database. Is there a way of doing this from within the IDE?

Mattias Andersson

8/14/2008 1:03:10 PM #

J. P. Piesco

Re: Mattias Andersson  |  7/2/2008 3:32:15 AM  
To get a changed script processed on the actual database you can use an SQL interface tool which will give you a user interface to run changed script on the actual database. myLittleAdmin for SQL 2005 is a free SQL interface application( www.mylittleadmin.com).
Maybe Helpful?

J. P. Piesco United States

9/5/2008 10:04:40 AM #

Richard

Nice overview of Database Projects, one of the few out there.

I'm trying to create and then query from a temp table.  Visual Studio seems to use a connection that drops with each round trip.  the create statement works, but the second select statement fails because the temp table no longer exists.

Have you run into this?  Any confirmation or suggestions?

Thanks again, Richard

Richard United States

9/5/2008 11:25:51 AM #

Ira

Thanks for the compliments Richard.

I have not run into that problem myself, but the connection re-setting and the scope being dropped would be a probable cause to your problem. I run into little things here or there with DB projects that make me pop over to Management Studio. But anything you do in Management Studio can easily be saved into the DB project for source control storage.

Sorry about not having a better solution, but it is what I use to get around it.

Ira United States

1/17/2009 3:50:49 AM #

Kay

Useful post, thanks.

Anyone know how to go the other way with multiple scripts? i.e. I have a folder in source control with all the objects scripted to separate files and would like to execute them against the same database without having to do each one seperately

Kay United States

3/11/2009 10:13:05 PM #

Mehdi Anis

I am using Visual Studio 2005 in my Win XP Pro development PC. Development PC has MS SQL EXPRESS and SQL Server Management Studio Express. I have MS SQL 2005 (Not free express) Server running in different location.

I followed your steps, but I do not see that 'Publish To Provider..." option in that pop-up-menu. Is that only for VS2K8? How to do the same thing in VS2K5?

Mehdi Anis United States

7/7/2009 8:44:25 AM #

Nathan Rozentals

Hey Ira,
I've used Visual Studio for Database professionals a fair bit, and it is an invaluable tool for team based database development.
Unfortunately, many of the clients that I work with cannot afford the price tag - and I cannot do without the functionality.
So I've been inspired to create an open-source project which is the "poor-mans" VS for DB Professionals.
http://dbsourcetools.codeplex.com/

Let me know what you think.
- Nathan.

Nathan Rozentals Australia

7/24/2009 3:49:13 AM #

Ruslan A.

Thanks.

Yes, it's good stuff for rollbacking DB!

Ruslan A. Uzbekistan

7/28/2009 10:22:12 AM #

David Wendelken

It's possible to edit a table via the server explorer.  You can right click when finished making your changes and ask it to make a sql script to alter the table.  The script will be automatically added to your project under change scripts.
I haven't tried it for other objects in the database yet...

I came up with a numbering system for my filenames in a prior project so that the files would automatically sort in the order in which I want to run them.  It's a small bit of bother to rename the files appropriately and it makes it really easy to go into DOS and put the list of files into a text file in the correct order, which can quickly be turned into a master script to run the files.  Hope that helps!

David Wendelken United States

7/28/2009 10:37:55 AM #

David Wendelken

What's the point of the Database Query object in the database proejct?

I guess it might be handy for remembering queries developers want to run against the database.  Anything else?

David Wendelken United States

7/28/2009 3:20:06 PM #

Ira

@David Wendelken

The Queries folder can be a very helpful tool. Especially if you have to write some "quick reports" that will be taken from a query to an excel sheet. By keeping them in the Queries folder and having version control on them, I can always revert to any previous form of a query I need.

Ira United States

9/29/2009 10:10:10 PM #

Clayton

What support do these projects provide for automated deployment?

We have rolled our own solution for doing this, which involves the developer basically just providing a list of SQL scripts to be executed in an MSBuild script, in the right order. The build engine then takes those scripts and produces an output package with all the files prefixed with a number to ensure the right order, and to allow simple review by a DBA. These scripts then get combined into a single master change script that wraps them all in a single block that adds error handling and transactionality if possible. We also do some hashing of the pre-installation database state and the scripts themselves to ensure that no one "fiddles" with things outside our controlled change process.

Does Database Projects provide support for a robust automated install?

To me this is a fairly fundamental part of the process.
Having the changes in scripts and in source control is fairly simple and does not really require much in the way of tooling. The more challenging part is managing the deployment of the changes properly, and there does not seem to be much out there in .NET land to support this, unless I am missing something.

Clayton United States

9/30/2009 8:00:11 AM #

Ira

No, this does not help with that. What I do is use a tool, like xSQLObject or SQLCompare to show and script the differences in the database and then I drop that script into my database project. This way I have a copy of what script is going to be deployed in my project. I wish there was a better way, but there isn't that I have found anyway.

Ira United States

10/13/2009 1:54:21 AM #

Pingle

Just wanted to share my experience. I do not use database-projects as there is too much manual handling involved. I have tried to make this a one-click thing by using nAnt to build my scripts and adding them to sourcecontrol. This way all I need to do at the end of a sprint and to get my objects synched is to click a button and then check in (in my current project we have hundreds of sps and functions etc, old stuff that I did not want to import manually file by file). I do not get all changes done on a daily basis, but at least I have a new version every three weeks or so. I have published my code here gardang.blogspot.com/.../...-database-objects.html
(One of the best things here is that each db-object has its own *.sql file and the tfs-synch will make sure that deleted items are indeed deleted from the source control)

Pingle Norway

10/15/2009 9:18:55 AM #

Joe

Can you help me how to deploy this Database project into different production Server ??

Thanks

Joe

Joe United States

10/15/2009 9:29:06 AM #

Ira

@Joe

If you right-click on a script in the db project, you can then select "Run On". There it will pop up a dialog asking what server you want to run the script on. If you don't see the server from the ones to choose then add it. That should get you going.

Ira United States

10/15/2009 9:52:23 AM #

Joe

Thanks Ira for the update,

i want to create to database project as above and generate scripts as above.
when i deploy in the client production environment, they run an exe and the database is created with the tables and all the database objects with it , is it posssible with VS 2008 database project.

One more question why is the Build menu (New Build Definition) greyed in the DB project

Thanks

Joe

Joe United States

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

IraIra
I'm just another developer from Florida.

 

Sponsors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

All code samples on this website are free for download, use, and modification with no warranty nor any implied warranty. Liscensed by:

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 United States License.