Solved

How could you update the connection string automatically during an install

Posted on 2010-11-16
6
434 Views
Last Modified: 2012-05-10
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>
0
Comment
Question by:Spike_66
  • 2
  • 2
  • 2
6 Comments
 
LVL 4

Expert Comment

by:alaran
ID: 34147408
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
 

Author Comment

by:Spike_66
ID: 34147583
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
 
LVL 4

Expert Comment

by:alaran
ID: 34147636
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34150444
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 34150452
If its always the local computer then you can use

".\SQLExpress" instead of "computername\SQLExpress" in the connectionstring.
0
 

Author Closing Comment

by:Spike_66
ID: 34154193
Simple!
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question