Captain Codeman Captain Codeman

Use Aliases to develop against SQL Server on different machines



This is a little tip that I’ve found useful when working on projects on different machines.

If you have a desktop machine and separate database server then you generally wouldn’t need to have SQL server running locally - either the full version OR the SQL Express edition.

So, within your app the connection string would reference the name of the server, e.g.:

  <add name="Library" connectionString="Data Source=MyServer;Initial Catalog=Library;Integrated Security=False;User ID=library;Password=secret;" providerName="System.Data.SqlClient"/>

The problem is of course when you checkout this code on another machine such as a laptop when working on-the-road … or just down in the basement while watching an episode of ‘Lost’ ;)

Sure, you can just change the config to say ‘(local)’ or ‘(local)SQL2005’ or whatever … but you run into issues with the file being changed and then having to change it back if you check it in.


The simplest solution I’ve found is to setup an Alias using the SQL Server Configuration Manager:

On the desktop machine, setup an alias called ‘dbserver’ pointing to the proper database server.

On the laptop machine, setup the same alias called ‘dbserver’ this time pointing to the local instance.

Now, the same connection string can be run on both machines (using ‘Data Source=dbserver’ in the connection string) without having to worry about changing it when checking it out and not checking it in if you changed it.

NOTE: I usually generate the database schema from the C# classes and NHibernate mapping file and include a data-setup tool so it isn’t an issue having two separate databases and normally most of the work is on the actual application and not so much on the database schema.