Link to home
Start Free TrialLog in
Avatar of KavyaVS
KavyaVS

asked on

Windows Application Connection String

I have Windows application with .Net 2.0
The connection string is hard coded in .exe of APP.Config file.I want to have some flexibility to change the connection string.
Is there any way to change the connection string  and how to access it in .exe file.
Please give detailed idea of how to do this.
SOLUTION
Avatar of dj_alik
dj_alik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KavyaVS
KavyaVS

ASKER

Can you please give me any code example.How to change  the connection string  in Windows application.
example in VB.NET?
Can you post App.config file?
Avatar of KavyaVS

ASKER

This is what exactly I am looking for.If possible vb.net code otherwise C# is also fine.
I want to add external configuration file.Storing the connectionStrings section in a physically separate file is useful in situations where connection strings may be edited after the application is deployed.
How to do this. Any code examples is appreciated.
Avatar of Ark
Public Class DBHelper
    Public Shared Property ConnectionString() As String
        Get
            Return My.MySettings.Default.MyConnectionString
        End Get
        Set(ByVal value As String)
            My.MySettings.Default.PropertyValues("MyConnectionString").PropertyValue = value
            My.MySettings.Default.Save()
        End Set
    End Property
End Class
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KavyaVS

ASKER

My.Settings.MyConString. What is My here.Is it Project name?

Thanks.
No. Its the My namespace of VB.NET which is available to every winforms app.

http://msdn.microsoft.com/en-us/vbasic/ms789188
Avatar of KavyaVS

ASKER

CodeCruiser

You can set its value at design time as well as at runtime.

How to set it at runtime?

Thanks
My.Settings.SettingName = Value
Avatar of KavyaVS

ASKER

Hi CodeCruiser,
I want to set the connection string automatically after deployment.My idea is the .exe file able to read the external text file in the application directory.
Based on the server type if it is '1' pointing to Development server and if it is 2 pointing to Production server.How to do this?

How to change the connection string automatically after deployment without changing it manually.


Thanks
You can either keep two connection strings in the settings and use the one suitable. Or you can change the connection string before deployment.

You can have 3 settings: Server1String, Server2String, MyConString

Then on form load of main form, if its server type 1 then set MyConString = Server1String etc.
Avatar of KavyaVS

ASKER

I don't want to change the form load of main form after deployment.
After deployment the executable able to know the server type from external file and uses the specific connection string.

Thanks
You dont change the form load after deployment. You change the connection string app will use in the form load.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial