none
Change ConnectionString in Application Settings RRS feed

  • Question

  • I have a C# application that uses a strongly typed DataSet and a number of TableAdapters to connect to a database. I want the user to be allowed to change the server and database name he wants to connect to.Windows authentication is being used, so there are no security concerns.

    I added a connection string to Settings.settings but it's added as Application level setting - thus it's read only. I don't want to have to change the connection string of each TableAdapter - there are a number of those.

    What's the recommended procedure? I've created a control that takes the server and database and builds the connection string, but I can't save it. I've searched but didn't find anything conclusive. Can I use any of the classes in the System.Configuration namespace to achieve that?

    Friday, September 2, 2011 3:11 PM

Answers

  • Hello VentsyV,

    Thanks for your post.

    According to your description, I think Paul's reply can help you a lot. And here is the method I quoted from Steve Lasker's blog:

    Customizing the connection string at runtime
    Developers often want to customize the connection string.  In most cases the password simply needs to be provided.  The strongly typed Settings file also has a few events that can be leveraged. 

    1. To add an event handler:
    2. Double click My Project in Solution Explorer
    3. Click the Settings tab on the left side
      In C#, double click the Properties folder in Solution Explorer
    4. Click the View Code toolbar in the top of the designer
    5. Select MySettings Events from the top left drop down and then select SettingsLoaded in the top right.

    Here’s a snippet I user for SQL Mobile to replace .\ with the data directory when running under click once.  This same code can be modified to replace the password of a connection string.

    Private Sub Settings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded

     

        Dim dataDirectory As String

     

        ' When running under debug mode, use the data file from the same directory as the executable

        ' otherwise, use the Data Directory which will be set for ClickOnce or MSI based installs

        ' This assumes that MSI based apps placed the data file in the standard non roaming data directory:

        ' C:\Documents and Settings\<UserName>\Application Data\<Company>\<Application Name>\<Version>

        If AppDomain.CurrentDomain.DomainManager IsNot Nothing AndAlso _

            AppDomain.CurrentDomain.DomainManager.ToString().Contains("VSHost") Then

     

            dataDirectory = Windows.Forms.Application.StartupPath

        Else

            dataDirectory = Windows.Forms.Application.UserAppDataPath 

         End If

     

        Me.Item("NorthwindConnectionString") = My.Settings.NorthwindConnectionString.Replace(".\", dataDirectory & "\")

     

    End Sub

     

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 7, 2011 5:34 AM
    Moderator

All replies

  • Looks like a duplicate of How to dynamically change connection string in generated dataset class



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Friday, September 2, 2011 7:22 PM
  • Hello VentsyV,

    Thanks for your post.

    According to your description, I think Paul's reply can help you a lot. And here is the method I quoted from Steve Lasker's blog:

    Customizing the connection string at runtime
    Developers often want to customize the connection string.  In most cases the password simply needs to be provided.  The strongly typed Settings file also has a few events that can be leveraged. 

    1. To add an event handler:
    2. Double click My Project in Solution Explorer
    3. Click the Settings tab on the left side
      In C#, double click the Properties folder in Solution Explorer
    4. Click the View Code toolbar in the top of the designer
    5. Select MySettings Events from the top left drop down and then select SettingsLoaded in the top right.

    Here’s a snippet I user for SQL Mobile to replace .\ with the data directory when running under click once.  This same code can be modified to replace the password of a connection string.

    Private Sub Settings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded

     

        Dim dataDirectory As String

     

        ' When running under debug mode, use the data file from the same directory as the executable

        ' otherwise, use the Data Directory which will be set for ClickOnce or MSI based installs

        ' This assumes that MSI based apps placed the data file in the standard non roaming data directory:

        ' C:\Documents and Settings\<UserName>\Application Data\<Company>\<Application Name>\<Version>

        If AppDomain.CurrentDomain.DomainManager IsNot Nothing AndAlso _

            AppDomain.CurrentDomain.DomainManager.ToString().Contains("VSHost") Then

     

            dataDirectory = Windows.Forms.Application.StartupPath

        Else

            dataDirectory = Windows.Forms.Application.UserAppDataPath 

         End If

     

        Me.Item("NorthwindConnectionString") = My.Settings.NorthwindConnectionString.Replace(".\", dataDirectory & "\")

     

    End Sub

     

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 7, 2011 5:34 AM
    Moderator