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.:

<connectionStrings>

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

 </connectionStrings>

 

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' Smile)

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.

Urgh.

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.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s