How could you update the connection string automatically during an install

What would be the best way to update a config file which contains a connection string to a SQL database  with the correct name of the machine.
i.e; Change line:
 <connectionStrings>
    <add name="DBConnectionString" connectionString="Data Source=ANY_LAPTOP\SQLEXPRESS;Initial Catalog=DB1;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

to

 <connectionStrings>
    <add name="DBConnectionString" connectionString="Data Source=MY_LAPTOP\SQLEXPRESS;Initial Catalog=DB1;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
Spike_66Asked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
If its always the local computer then you can use

".\SQLExpress" instead of "computername\SQLExpress" in the connectionstring.
0
 
alaranCommented:
If you mean during install from an .msi package, you should look into "Custom Actions" for the installer project. These allow you to execute pretty much whatever you want during installation, including manipulating the .config file.

The connection string can then be asked for during installation and inserted into the config file during installation.

Post number two here has a sample: http://stackoverflow.com/questions/918565/updating-appname-config-file-from-an-custom-installer-class-action
0
 
Spike_66Author Commented:
This is good but the user is unlikely to know the connection string.
This would be for an application connecting to an existing application on a SQL server database.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
alaranCommented:
The custom action can also get the connection string from another place if that is possible. (like web service, file, system information or whatnot)
0
 
CodeCruiserCommented:
Is it just the server name that would be dynamic or the instance name and db name as well? Is the server expected to be local or network?
0
 
Spike_66Author Commented:
Simple!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.