An object-oriented programming language developed by Microsoft that can be used in .NET.
Thanks for reaching out.
You’re correct to focus on |DataDirectory|—the key limitation is that it’s global to the AppDomain, so once you set it, every connection string using |DataDirectory| resolves to the same folder. That’s why you can’t have common.mdf in the EXE folder and variable.mdf in a user-chosen folder using two different substitution tokens. [stackoverflow.com], [dori-uw-1.kuma-moon.com]
Good news: using “dynamic connection strings” does not mean you must stop using TableAdapters or rewrite CRUD SQL. TableAdapters already expose a Connection object, and you can simply set TableAdapter.Connection.ConnectionString (or TableAdapterManager.Connection.ConnectionString) at runtime and continue to use the existing Fill/Update/Insert/Delete methods as-is. [stackoverflow.com], [Directly a...soft Learn | dori-uw-1.kuma-moon.com], [Update dat...soft Learn | dori-uw-1.kuma-moon.com], [The New Ta...soft Learn | dori-uw-1.kuma-moon.com]
Example (VB.NET) – keep TableAdapters, point each DB to its correct folder
Imports System.IO
Dim commonDbPath As String = Path.Combine(Application.StartupPath, "common.mdf")
Dim variableDbPath As String = Path.Combine(userSelectedFolder, "variable.mdf")
Dim commonConnStr As String =
$"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename={commonDbPath};Integrated Security=True;Connect Timeout=30"
Dim variableConnStr As String =
$"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename={variableDbPath};Integrated Security=True;Connect Timeout=30"
' Common DB adapters
Me.CommonTableAdapter.Connection.ConnectionString = commonConnStr
' Variable DB adapters
Me.VariableTableAdapter.Connection.ConnectionString = variableConnStr
About “external config per client”
.NET does support external config fragments for <connectionStrings> using configSource. However, configSource has a restriction: it must refer to a file in the same directory or a subdirectory of the main app config—so it won’t directly point to an arbitrary user-selected client folder. [dori-uw-1.kuma-moon.com] [stackoverflow.com]
So the practical model is either:
- Store the per-client connection string in a small XML/settings file in the client folder, read it at startup, and assign it to the variable TableAdapters (same as above), or
- Keep the connection strings in app settings and just update the variable connection string at runtime.
Let me know if you need any further help with this. I will be happy to assist. If you find this helpful, Kindly mark the provided solution as "Accept Answer", so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.