Question

I am creating a WPF application that reads ConnectionString that is created at runtime in the AppDomain. It works perfectly, until I start using DataSets, since they point to the default app.config file in my Application Root.

When I first load the App, the connString gets saved as a Variable for later use. My first intuition was to use that Variable for my connection. Since this would mean less IO read7write and thus making the system faster. I was able to get as far as to change the connection string, but somewhere down the line I need to initiated and didn't know where.

Now I am trying to figureout, how can I get the Dataset (or Settings) file to read the app.config located in the AppDomain instead of the default one located in my Application Root Folder.

FYI, I creating a Buisness Data Centric App. The Connection String changes depending on each company's internal configuration, so its necesary to have a variable connection string. Again this I have done.

My main goal is to centralize the data in a Variable or a Single File as this will cause less headaches down the road.

Thanks in advance!

Was it helpful?

Solution

basically I was able to solve the proble:

In case you are wondering what the problem actually was: I didn't want the code to read a app.config file to retrieve the ConnectionString. Since when I start the system, I read it once, it makes little sense to continue reading it through out the use of the system.

BTW, THIS IS WPF APP WITH SQL SERVER 2012 DB.

So this is how its done:

STEP 1: Go to Settings (Project > Settings) and create a new item called DBConnString (select ConnectionString as Type and Application as Scope). And type in some defualt ConnectionString. The entered string must be real, as the Datasets will read this during design time.

STEP 2: Hit F7 on the Settings Page to view the Code Behind. Type in this Code:

Partial Friend NotInheritable Class MySettings

Private Sub MySettings_SettingsLoaded(sender As Object, e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
    Me.Item("DBConnString") = ConnStringVariable
End Sub End Class

Everytime any part of your Project wants access Settings class, this code will fireup at the Loaded Event. Meaning that before anybody can read the default connection string, it will be changed. In case your a newbie, please replace ConnStringVariable with your actual variable.

STEP 3: Create a Dataset and DataTable, and when asked to connect, you will see a new option: DBConnString (MySettings). Select that.

Remember that During Design Time, the DataTables will use the Default ConnString you wrote manually, but during RunTime the Settings.vb (or cs) class will kick in replacing the default by the variable.

This simple trick will allow you to use a ConnectionString Variable to be reused throughout the whole project not having to access the disk everytime it tries to connection to the DB!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top