We help IT Professionals succeed at work.

vb.net build connection at first run

miller75
miller75 asked
on
Medium Priority
217 Views
Last Modified: 2012-05-11
I have developed a small app with the connections string to a SQL database in the .config file, this is fine on my development machine but when I try to deploy it on another machine it obviously needs changing, as the config file is read only at runtime what is the best way of handling the connection change.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2015

Commented:
Just change the web.config file on the server where the web application is deployed. You can even do this while the sit is live. Or, upload a corrected one to replace the original.

Author

Commented:
Sorry didn't say it was a windows app
CERTIFIED EXPERT
Top Expert 2015

Commented:
If the string is embedded in the code then you will have to correct and recompile. The better way to handle it would be to put your connection strings in the app.config file. Works the same as a web.config file.
CERTIFIED EXPERT
Top Expert 2015

Commented:
Here's a sample local sql connection entry in the app.config file:


    <connectionStrings>
        <add name="myWinApp.My.MySettings.myDbConnectionString"
            connectionString="Data Source=machineName;Initial Catalog=myTable;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

Place this in the configuration section of the app.config file.
CERTIFIED EXPERT
Top Expert 2015

Commented:
Very easy to access in code using MySettings:

Dim SqlConnectionStr As String = My.Settings.myDbConnectionString

You can store any kind of data that is likely to change over time in the app.config file like passwords or email addresses, etc.

Author

Commented:
This is what I am currently using, the problem I have is if I send the app to the end user I want them to be able to build the connection string rather than having to edit the app.config file manualy
CERTIFIED EXPERT
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
You can change the scope of the setting to User rather than application and then its not readonly. Then the user can change it on the Settings form within your app and you can modify it from within your code.

My.Settings.ConStr = "New con str"
My.Settings.Save()

Commented:
Hi!

Why not use xml, to store connection string.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.