Create an SQLExpress database using Visual Studio

This took me a bit of time to figure out – how to create an empty SQL express database without using SQL Mangement Studio (I didn’t have it installed..) but instead using Visual Studio 2010 (and without having to add data sources, model definitions and other such muck into the context of a VS project)

 

Once you have a database you can use Visual Studio’s ‘Server Explorer’ pane to edit the DB and add tables etc. etc. but it isn’t obvious how to create an empty database in the first place, so in the end I did the following:

 

1.) Check that the SQL services are running – start them if necessary.

 

2.) Use Visual Studio to connect to the SQL Express master database – Choose the Tools / Connect to Database menu item. Specify ‘Microsoft SQL Server’ as the data source. Enter ./SQLEXPRESS as the ‘Server name’, or hit the browse button if you the have the browser service running. Leave the database name field blank. Press the OK button and VS should connect to the local master database, you should see it in the ‘Server Explorer’ pane.

 

3.) Now right click on the master database in the server explorer pane and choose the ‘New Query’ menu item. In the query window type:

 

[crayon]
create database DatabaseName
[/crayon]

 

Where DatabaseName is the name of the database that you want to create. Execute the query (press Ctrl – E) – this should create your new database.

 

3.) To connect to your new database repeat step 2.) and specify the database name – Visual Studio should now connect to it. Now you can add tables etc.

 

4.) You can now remove the connection to the master database in the Server Explorer pane if you like.