Change DB connection

I'm developing a windows application based on an Access database. I used the wizard to create the table adapters, relations and so on and all works fine.
But using the wizard the connection to the database is hide, the system does the connection by itself. It supposes that the db is stored always in the same location at the same path.
I need to use the application changing as needed the database (same structure different data obviously). My goal is to have a menu item like "Change db" where I can browse the file system and choose the db.
Can I change the connection of the dataset at runtime How can I do

Dado



Answer this question

Change DB connection

  • JT Snake

    ok, I saw my app.config and the Connection String is stored there. But how can I change it I have an MDI application and if I try to use the code in a Click event of a menu item it doesn't work because it cannot find the ConnectionString object.

    Sorry to bore you again...

    Dado

  • Tom Morris

    DMan1 thanks for your reply but the problem is that the Connection is transparent for me, it created by the system automatically and I cannot reach it.

    Dado

  • Tiadd4

    My.Settings.MyConnectionString="BLAH"

    Where MyConnectionString is the setting name.



  • Cuzco

    even when you use the visual tools (ie drag and drop a table on a form) a connection object is created and the connection string is (by Default) stored in app.config :  If you take a look at your data object and look at the data source there will be the name of the data source which has a connection associated with it...hope that helps..

  • davherb

    I found a link on the forums:
    https://blogs.msdn.com/smartclientdata/archive/2005/07/25/443034.aspx

    Basically I had a situation where I wanted to change the connectionstring that M$ store when using the funky project data sources way of coding.

    All I did is added this sub to the settings.vb file (instructions how to view the setting code in included in that link above)

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

    Me.Item("cnDatabase") = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.Item("UserSettingDatabaseLocation")

    End Sub

    Where cnDatabase is the name of the Connection you have used for your datasets, and UserSettingDatabaselocation was just a User setting that i have used, but you could set that to anything like your own XML based setting or anything.

    Good Luck!



  • dandoyon

    All you need to do is change the connection string of the connection object!

    MyCOnnection.ConnectionString="Blah,Blah"
    MyDataAdapter.Connection = MyConnection



  • Amer 570

    it my app I see that the my.settings has the connection name in it: cnDBSource

    however it is readonly, how do you change it at runtime

    Thanks for you help.

    Regards,

    Michael Proctor



  • Change DB connection