VS 2010 Database Project: An Introduction

Posted Sun, May 2 2010 17:54 by Deborah Kurata

Visual Studio 2010 has a new Database Project. The scripts in this new Database project define your database just like the files in your code projects define your application.

The prior Database project (the one under the "Other Projects" node in VS 2003/2005/2008 as detailed in this prior post), was simply a storage container for your database scripts.

The new Database project is alive. Like the old Database project, it retains your database scripts, but it also has Intellisense, build-time validation, and automatic deployment features. It allows you to keep the definition of the database in your scripts and ensure that the scripts are deployed to your development database.

This is the first in a series of posts on using the new Database project. This post provides the steps for adding a Database Project to any solution in either VB or C#.

1. Right-click on your solution and select Add | New Project. Or select File | New Project from the main menu.

The Add New Project dialog is then displayed.

image

2. Open the Database node in the Installed Templates panel on the left and select SQL Server.

3. Select SQL Server 2008 Database Project from the middle panel, enter a name for the project, and click OK.

NOTE: If you have SQL Server 2005, you can select the SQL Server 2005 Database Project instead.

Visual Studio then creates the new project and adds it to your Solution Explorer:

image

4. Use the Database project to write the scripts for a new database or manage the scripts for an existing database.

For example, use the Tables node to manage your table scripts and the Programmability | Stored Procedures node to manage your stored procedure scripts:

image

These posts provide details on using this new Database project:

Enjoy!

Filed under: , , , , ,

Comments

# VS 2010 Database Project: Using an Existing Database

Sunday, May 02, 2010 8:20 PM by Deborah's Developer MindScape

The new Visual Studio 2010 Database Project, as described in this prior post , makes it easy to work

# re: VS 2010 Database Project: An Introduction

Monday, May 03, 2010 12:02 AM by susan

Hiiiiiiiiiii everyone ,

I like your site as well as your blog as it had provided a good information on the technology.

============

Susan

<a href="http://compareisas.org.uk" rel="dofollow">Compare ISAs</a>

# VS 2010 Database Project: Adding Tables

Monday, May 03, 2010 12:43 AM by Deborah's Developer MindScape

The new Visual Studio 2010 Database Project, as described in this prior post , makes it easy to work

# VS 2010 Database Project: Building and Deployment

Monday, May 03, 2010 12:52 AM by Deborah's Developer MindScape

The new Visual Studio 2010 Database Project, as described in this prior post , makes it easy to manage

# VS 2010 Database Project: Adding Stored Procedures

Monday, May 03, 2010 1:37 AM by Deborah's Developer MindScape

The new Visual Studio 2010 Database Project, as described in this prior post , makes it easy to work

# Interesting Finds: May 3, 2010

Monday, May 03, 2010 6:10 AM by Jason Haley

Interesting Finds: May 3, 2010

# re: VS 2010 Database Project: An Introduction

Monday, May 03, 2010 5:43 PM by Kevin

Hey there,

We've tried to use Database Projects in the past but ran into circular dependency issues. Our SqlServer has a dozen databases with lots of sprocs/udfs that perform cross database joins, selects, inserts, etc.

The problem happens when Database A is "compiling" and it references Database B which causes Visual Studio to "compile" Database B which has a reference to Database A... rinse and repeat. Circular.

Does Visual Studio 2010 handle circular references more effectively? Other than getting rid of the circular references (which I'd love to do, if possible), do you have any suggestions?

Thanks.

-k

# re: VS 2010 Database Project: An Introduction

Tuesday, May 04, 2010 1:32 AM by gregor suttie

I didnt get this as part of the install - damn I knew I should have chosen install everything (even though I really didn want to) - so what should I check to include these templates when doing a new install?

# re: I didn't get this as part of the install

Tuesday, May 04, 2010 4:27 PM by Andy

I also thought I didn't have the templates installed.  If I clicked on 'Database', the list to the right would say, "No Items Installed" or something like that.  However, clicking the small navigational arrow to the left of the word 'Databases' opens the child menu 'SQL Server'.  That's where all the templates are.  Maybe you're having the same problem.  Hope this helps.

# re: VS 2010 Database Project: An Introduction

Thursday, May 06, 2010 5:10 PM by Eduardo

There is any way to make it dumb again?

I only want to store my scripts.

# re: VS 2010 Database Project: An Introduction

Thursday, May 06, 2010 8:02 PM by Deborah Kurata

Hi Eduardo -

There is no way to dumb it again AFAIK. That was why I was a little disappointed with this whole thing. To me, this *new* database project should have been an addition to instead of a replacement for the old database project for those just needing a simple solution and not the complexity of the new one.

You can just store your scripts in the new database project. And if you upgraded a prior version project with an old database project, that is what it will do.

But you won't get any of the easy features, like "Generate Script to Project". I think I miss that one the most!

# re: VS 2010 Database Project: An Introduction

Thursday, May 13, 2010 9:09 AM by Gennadiy Chornenkyy

Hi "-k"

I think it could be helpful - MSDN link "Starting Team Development of Databases that Reference Other Databases"

msdn.microsoft.com/.../dd193279.aspx

Good luck

G.

# re: VS 2010 Database Project: An Introduction

Thursday, May 27, 2010 7:25 AM by Eduardo

Deborah: What I really miss from the old DB Project, is that every script in that project will run against the database selected for the project.

Now I have to Connect to the Server and then select the correct DB (or have "USE XX" in every script)

# re: VS 2010 Database Project: An Introduction

Thursday, May 27, 2010 10:35 AM by Deborah Kurata

Eduardo -

If you follow the steps here:

msmvps.com/.../vs-2010-database-project-building-and-deployment.aspx

you should not have to connect each time.

Hope this helps.

# re: VS 2010 Database Project: An Introduction

Thursday, May 27, 2010 4:24 PM by Noam Ben-Ami

It is possible to have circular references in database projects: Read up on "Partial Project References". Essentially, you can export part of a project as a file and reference it from another project, which allows that project to resolve references.

# re: VS 2010 Database Project: An Introduction

Friday, June 11, 2010 6:05 AM by Eduardo

Deborah: I'm not sure which part or the post are you referring.

I've changed the deployment setting and setup the target database (scared as hell), but the script do not connect automatically to that DB when I opened them in VS2010.

Missed something?

# re: VS 2010 Database Project: An Introduction

Friday, June 11, 2010 9:34 AM by Deborah Kurata

Hi Edwardo -

No, the scripts do not connect automatically when you open them in Visual Studio. I complained about this too. So if I want to execute them manually, I have to connect each time.

But if you set up the project as per the post link I provided, it will automatically connect and execute the changed script when you build/deploy the project.

Hope this helps.

# re: VS 2010 Database Project: An Introduction

Friday, June 11, 2010 11:20 AM by Eduardo

Thanks Deborah, it's clear now.

I'm not into automatic deploying scripts, so it's useless to me.

But thanks anyway.

# re: VS 2010 Database Project: An Introduction

Friday, June 11, 2010 8:12 PM by Deborah Kurata

Hi Eduard -

Yea, it didn't feel right to me at first either. But now that I started deploying the script at compile time, I have gotten to like it.

Good luck!

# re: VS 2010 Database Project: An Introduction

Tuesday, June 15, 2010 2:52 PM by Eduardo

And now in top of bad news: The editor can't connect to SQL 2000!!

# Záložky z prohlížeče – 29.6.2010

Tuesday, June 29, 2010 12:34 AM by .NET i jiné ...

Aglight.cz – blog o WPF a SL v češtině Nejlepší programy pro vylepšování HTML kódů – přehled validátorů,

# re: VS 2010 Database Project: An Introduction

Tuesday, June 29, 2010 1:26 PM by Leon

Hi Debs, thanks so much for these very informative articles. I've just migrated to VS2010 and the new dbproj throws a few spanners in the works for me but I can see with a different approach it can really make life a lot easier. Your articles have provided an easy and quick way to get up to speed, thanks again!

# VS 2010 Database projects - treat your database as code

Thursday, July 29, 2010 7:19 PM by Geek #

VS 2010 Database projects - treat your database as code

# re: VS 2010 Database Project: An Introduction

Monday, December 20, 2010 2:17 AM by steve mandel

I see where you can execute a stored procedure in order to store it in the database , but in the old database projects you could also run the stored procedure - is this also gone

# re: VS 2010 Database Project: An Introduction

Monday, December 20, 2010 11:35 AM by Deborah Kurata

Hi Steve -

You can highlight the Select statement in your stored procedure from within the Database Project, right-click, and select Execute. It will then run your stored procedure. However, it does not bring up a UI for entry of your parameters. So you can only use this technique when your stored procedure takes no parameters.

Alternatively, you can run your stored procedure from the Server Explorer window. Then it displays a UI for entry of your parameters.

Hope this helps.

# re: VS 2010 Database Project: An Introduction

Tuesday, December 28, 2010 2:04 PM by Paul

Deborah,

We have been trying to use a database project in our enterprise application (VS 2010 and SQL Server 2008 R2).  However, doing a schema compare and write changes results in the generation of horrific scripts.  

For example,

CREATE TABLE [dbo].[LabGroups] (

   [LabGroupUid]          UNIQUEIDENTIFIER NOT NULL,

   [FK_ParentLabGroupUid] UNIQUEIDENTIFIER NULL,

   [FK_LabGroupLeaderUid] UNIQUEIDENTIFIER NULL,

   ALTER TABLE [dbo].[LabGroups]

   ADD [LabGroupCode] NVARCHAR (20) NOT NULL;

,

   [LabGroupName]         NVARCHAR (120)   NOT NULL,

   [IsLab]                BIT              NOT NULL,

   [Created]              DATETIME         NOT NULL,

   [CreatedBy]            NVARCHAR (50)    NOT NULL,

   [Modified]             DATETIME         NOT NULL,

   [ModifiedBy]           NVARCHAR (50)    NOT NULL

);

We get large regions of the scripts commented randomly and also odd mixtures of CREATE and ALTER <object> as in above ... my database guy is spending hours to fix the scripts after every compare.

Have you seen anything like this?

# re: VS 2010 Database Project: An Introduction

Tuesday, January 11, 2011 7:36 PM by Tim Lajaunie

Nice article!

Question:  Internally, I plan to use a 2008 database project.  However, several of my customers still use 2005.  Will a dbschema built with a SQL Server 2008 project be able to update a 2005 database out in the wild with VSDBCMD.exe?

Thanks.

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: