In the last post, I showed how to create a Db project and create SQL tables by hand, or at least start by hand, and finess them via the Schema View.

 

That’s nice… but most times I don’t start from scripted Databases, and instead whack at databases traditionally, in SQL Server Manager (or now using EF4) and end up needing to import them into the database, where I finesse them as I go along.

 

So here’s an example of how to do that.

In Visual Studio, we can right click our Db project and simply import an existing Database:

 

image

image_thumb[9]

And you get a huge warning here that I want you to remember (notably the ‘master’ part):

image_thumb[10]

But click yes anyway, and then it goes to work…finishing up a little while later with a report:

image_thumb[11]

About now, if you look at the project’s files now you’ll see that the DDL of the tables (as well as other things, such as constraints) has been created:

image

That’s great. We have the DDL, and yes, in this example, we’ve imported from a db, so we know a db exists…but the whole point of the exercise is to have a DDL script to deploy to a db that doesn’t have a database – or if it has one, update it. So let’s give it a whack…

In Solution Explorer, rick click the project, in order to bring up its Properties, and from there go to the build tab:

image

WARNING: Notice the name of the db it is going to use…yeah, master…now you know why it gave you that whopping big warning up front (you certainly do NOT want to create tables in the master db).

Enter the name of the db to create, or update. In our simple demo, we’ll use the name the db we imported (just to keep my server somewhat clean) but you get the point – the operation is the same either way.

First ensure we actually are going to deploy, and not just generate scripts:

image

and second, edit the connection string via the popup dialog:

image

 

You’re now all ready to try: right click the project and deploy it:

image

If the SQL that is in the project is all good, you’re good to go!

image

powered by metaPost