• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

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.
0
KavyaVS
Asked:
KavyaVS
  • 6
  • 5
  • 3
  • +1
3 Solutions
 
dj_alikCommented:
APP.Config is  stabdalone  xml/textual  file and does not part of application  EXE
if you used app.config in project try to  find by name APPEXEANEM.EXE.CONFIG in application directory.
0
 
KavyaVSAuthor Commented:
Can you please give me any code example.How to change  the connection string  in Windows application.
0
 
dj_alikCommented:
example in VB.NET?
Can you post App.config file?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
KavyaVSAuthor Commented:
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.
0
 
ArkCommented:
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
0
 
CodeCruiserCommented:
Open project properties, goto Settings tab. Add a new setting of type String with "User" scope.

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

Now to use it

Dim dbcon As New SqlConnection(My.Settings.MyConString)


You can also change it from within your app (through an Options form may be)

My.Settings.MyConString = "New connection string"
My.Settings.Save()

You can also modify the myexename.exe.config file manually to change the connection string.


A positive side affect of this approach would be that you would only need to change the connection string in one place.
0
 
KavyaVSAuthor Commented:
My.Settings.MyConString. What is My here.Is it Project name?

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

http://msdn.microsoft.com/en-us/vbasic/ms789188
0
 
KavyaVSAuthor Commented:
CodeCruiser

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

How to set it at runtime?

Thanks
0
 
CodeCruiserCommented:
My.Settings.SettingName = Value
0
 
KavyaVSAuthor Commented:
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
0
 
CodeCruiserCommented:
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.
0
 
KavyaVSAuthor Commented:
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
0
 
CodeCruiserCommented:
You dont change the form load after deployment. You change the connection string app will use in the form load.
0
 
ArkCommented:
Why not allow end user choose connection himself?
Add references to Microsoft.Data.ConnectionUI.dll and Microsoft.Data.ConnectionUI.Dialog.dll (you can find them at %Visual Studio Folde%\Common\IDE. You'll also need include them is distributable package.).
First, check if initial connection string is OK, smth like this

 
Public Function CheckConnection(ByVal strConnection As String) As Boolean
        Dim conn As SqlConnection = New SqlConnection
        conn.ConnectionString = strConnection
        Dim bSuccess As Boolean = False
        Try
            conn.Open()
            conn.Close()
            bSuccess = True
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
        conn.Dispose()
        Return bSuccess
    End Function

Open in new window

If not - prompt user to select correct data source:
 
Dim connDlg As DataConnectionDialog
connDlg = New DataConnectionDialog
DataSource.AddStandardDataSources(connDlg)
connDlg.SelectedDataSource = DataSource.SqlFileDataSource
connDlg.ConnectionString = My.Settings.DBConnectionString
If DataConnectionDialog.Show(connDlg) = Windows.Forms.DialogResult.OK Then
      If CheckConnection(connDlg.ConnectionString) Then
            If SetNewConnectionString(connDlg.ConnectionString, "DBConnectionString") Then
                  Me.Cursor = Cursors.WaitCursor
                  InitConnection()
                  Me.Cursor = Cursors.Default
              End If
        End If
End If

Open in new window

Check Connection again, if it's OK:
SetNewConnectionString(connDlg.ConnectionString, "DBConnectionString") - replace connection string in app settings using DBHelper shared property from my previous post )I didn't test it or smth like this (tested)
 
Private Function SetNewConnectionString(ByVal connString As String, ByVal connName As String) As Boolean
        Try
            Dim _config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
            Dim _section As ConnectionStringsSection = CType(_config.GetSection("connectionStrings"), ConnectionStringsSection)
            Dim _connectionFullName As String = String.Format("{0}.My.MySettings.{1}", "MyAppNameSpace", connName)
            Dim _settings As ConnectionStringSettings = _section.ConnectionStrings(_connectionFullName)
            If _settings Is Nothing Then
                _settings = New ConnectionStringSettings(_connectionFullName, connString, "System.Data.SqlClient")
            Else
                _settings.ConnectionString = connString
            End If
            _config.ConnectionStrings.ConnectionStrings.Clear()
            _config.ConnectionStrings.ConnectionStrings.Add(_settings)
            _config.Save(ConfigurationSaveMode.Full)
            ConfigurationManager.RefreshSection(_section.SectionInformation.Name)
            My.MySettings.Default.Item("DBConnectionString") = connString
            ConfigurationManager.AppSettings("DBConnectionString") = connString

            My.MySettings.Default.Save()
            My.MySettings.Default.Reload()
            Return ResetConnectionString(connString)
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        End Try
    End Function

Open in new window

Then, reset connection string for all adapters and process your form
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now