Link to home
Start Free TrialLog in
Avatar of KJLC
KJLC

asked on

changing a connection string in a library class at run time vb.net

So I have a solution split into several projects.  The main project being a windows forms project.  The problem is that one of my class libraries contains a connection string that is pointing to a sqlite database in the application directory.  Currently it is an absolute path but I would like to change it to based on what the current application directory is.  I am able to construct the path using my.application.info.directorypath but setting the application.setting won't work from what I understand due to it being a Class Library. and no dll.config file.   The actual connection string is used by multiple table adapters as defined in the .xsd files and the code visual studio has created for the basic functionality.

Hopefully there is a way around this.  At the moment every change of install path requires updating the settings pre-build which won't work moving forward when we pull together an installer for this.

Thanks in advance!
Avatar of Snarf0001
Snarf0001
Flag of Canada image

You can still use application settings for what you want.
If you go into Properties -> Settings of the ClassLibrary, click the Create settings option and put in the setting you want.

In the app.config of the winform application, you add the reference to those settings in there.  You need to add the section in <configSections> and in <applicationSettings>.

Once it's in there, the class library will pull it's values from the main config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="MyClassLibrary.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      <section name="MyWinfowsForm.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <MyClassLibrary.Properties.Settings>
      <setting name="ClassLibSettings" serializeAs="String">
        <value>fff</value>
      </setting>
    </MyClassLibrary.Properties.Settings>
    <MyWinfowsForm.Properties.Settings>
      <setting name="WinFormSetting1" serializeAs="String">
        <value>eee</value>
      </setting>
    </MyWinfowsForm.Properties.Settings>
  </applicationSettings>
</configuration>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Or, you can find the entry point assembly from your class library, and the access its connection strings directly:
    Public Function GetConnString(ByVal connectionName As String)

        Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetEntryAssembly()
        Dim config As System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(asm.Location)

        Return config.ConnectionStrings().ConnectionStrings(connectionName).ConnectionString

    End Function

Open in new window

Avatar of KJLC
KJLC

ASKER

I'm not sure why I didn't think of that but it is working well for my needs.  I ended up passing the updated connection strings into a sub in the dll class that runs through the local setting updates.