There are several reasons I prefer using Visual Studio for maintaining the design and functionality of a SQL Server database. The two top reasons are the source control integration and the ability to easily deploy an instance of the database with test data. There’s also great functionality like schema and data compare but those are out of scope for this post.
When designing a database from scratch I like to use tools other than Visual Studio. Sometimes I just work in script or use the built in design functionality from SQL Server Management Studio. The vast majority of the time though I use a third-party tool like Sparx Systems’ Enterprise Architect. I really like the visual design surface that EA offers and it’s also great when you need to reverse engineer an existing database.
So my problem was that I needed to be able to create a Database Project from an existing database design in SQL Server. The solution ended up being a pretty simple three step process.
First, create a new Visual Studio 2010 Database Project.
Once you’ve got the project created you next need to add a data connection to our existing SQL Server database. This connection will allow you to maintain a relationship between the Visual Studio project and the database it targets. Once the connection is added you will be able to drill down and view the various database objects.
Now that the project is connected to the target database you can generate all selected database objects into your VS Database Project. Right-click on your project and choose “Import Database Objects and Settings…”. It’s important to note that you can just as easily import from a script file which in some cases might be your only or preferred option depending on your level of access to the target database. The script import process is identical with the exception of a script file not a database connection being the input.
Once the import process is complete you will have scripts for your database objects as part of your Database Project that you can check into source control, use to deploy an instance of your database, etc! Combine these scripts with a script to generate test data and you have the perfect way to share a development database in a team environment.
