Friday, August 12, 2011

How to redirect SQL Server without modifying .NET config file

For desktop .NET application, .config file is automatically generated from App.config. That is, every time developers rebuilds the .NET application/solution,  new *.exe.config file is generated from the App.config file. Here is an example of connection string section of App.config file:

<add name="MyConnectionString"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True"
providerName="System.Data.SqlClient" />

Once .exe.config file is generated, one can modify the .config file to change SQL Server name. Let's say one might want to put test SQL Server name. So this can be done manually, which is tedious way. Or one can add post build script to overwrite .config file.

There is another way - which is redirecting SQL Server to other server by using SQL Server alias. In some circumstances, this might be a better way.

Here is an example of how to redirect .\SQLEXPRESS server to TESTSERVER1 server.

(1)   Run SQL Server Configuration Manager from Windows [Start] menu

(2)   Expand [SQL Native Client 10.0 Configuration] -> [Alias]

(3)   Click [New Alias] and type [Alias Name] and [Server] as follows.

(4)   Click [OK]. Now the application will redirect .\SQLEXPRESS to TESTSERVER1 server.

No comments:

Post a Comment