Changing DataDirectory does not load different data
When the user clicks File | Open, the user gets a FolderBrowserDialog to select a folder containing the MyData.mdf data file. After clicking OK, the app calls
AppDomain.CurrentDomain.SetData("DataDirectory", FolderBrowserDialog.SelectedPath)
OrganizationTableAdapter.Fill(MyDataSet.Organization)
On first run of the app, it works as expected. The data displayed is the data from the MyData.mdf file in the FolderBrowserDialog.SelectedPath folder.
But when the user subsequently clicks File | Open and selects a different folder, while same code executes to change the DataDirectory and Fill the OrganizationTableAdapter, the data displayed remains the same data as the first folder that was opened. The only way to open a different dataset is to exit the app and launch the app again.
OrganizationTableAdapter.ClearBeforeFill is set to True, so I expect the old data to be cleared out and the data from the new directory to be loaded in. That's not what is happening.
I don't understand what is happening or how to fix it.
4 answers
-
Nancy Vo (WICLOUD CORPORATION) 5,865 Reputation points • Microsoft External Staff • Moderator
Hello @James Buss ,
Thanks for your question.
I recommend investigating the connection string resolution and connection pooling behavior. The DataDirectory token in the connection string appears to be resolved during the initial connection establishment. ADO.NET's connection pooling likely caches this connection with the originally resolved path. When you update the DataDirectory value, I suspect the pooled connection continues referencing the initial database file instead of re-evaluating the token with the new path.
You can refer to my following example code:
Imports System.Data.SqlClient Private Sub LoadDatabaseFromFolder() If FolderBrowserDialog1.ShowDialog() <> DialogResult.OK Then Return If OrganizationTableAdapter.Connection IsNot Nothing Then OrganizationTableAdapter.Connection.Close() End If OrganizationTableAdapter.Dispose() SqlConnection.ClearAllPools() AppDomain.CurrentDomain.SetData("DataDirectory", FolderBrowserDialog1.SelectedPath) OrganizationTableAdapter = New OrganizationTableAdapter() MyDataSet.Organization.Clear() OrganizationTableAdapter.Fill(MyDataSet.Organization) End SubI hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.
-
Senthil kumar 685 Reputation points
Hi @James Buss
try this below code will helpful to you.
Imports System.Data Imports System.Data.SqlClient Public Class Form1 Dim ofd As New OpenFileDialog Dim conn As SqlConnection Dim cmd As SqlCommand Dim tableadapter As SqlDataAdapter Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click ofd.Filter = "MDF Files (*.mdf)|*.mdf" If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then Dim fileName As String = ofd.FileName Dim Connstr As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename={" & fileName & "};Integrated Security=True;Connect Timeout=30;" Using conn = New SqlConnection(Connstr) conn.Open() Using tableadapter = New SqlDataAdapter("yourquery or storedprocedure", conn) // here do your stuff.. End Using conn.Close() End Using End If End Sub End Class -
James Buss 136 Reputation points
This makes sense. So, after calling
SetData("DataDirectory", ...), how do I force theTableAdapterto use a newSqlConnectionbuilt from that connection string? What code do I need to do that? The TableAdapter is created in the Designer not through code. -
AI answer
AppDomain.CurrentDomain.SetData("DataDirectory", ...)only affects how connection strings that use|DataDirectory|are built when the connection is first created. Once theTableAdapter’s underlyingSqlConnectionhas been created and opened, changingDataDirectorydoes not change the connection’sConnectionString, nor does it force the adapter to rebuild or reopen it.Result: after the first
Fill, theTableAdapterkeeps using the original connection (pointing to the firstMyData.mdf), so subsequentFillcalls still read from the first database even thoughDataDirectoryhas been changed.To load data from a different folder without restarting the app, the connection must be recreated (or its connection string updated) after changing
DataDirectory. Typical patterns:- Ensure the connection string in the
TableAdapteruses|DataDirectory|. - After calling
SetData("DataDirectory", ...), force theTableAdapterto use a newSqlConnectionbuilt from that connection string, or dispose and recreate theDataSet/TableAdapterinstance so that a new connection is created using the updatedDataDirectory. - Then call
OrganizationTableAdapter.Fill(MyDataSet.Organization).
ClearBeforeFill = Trueonly clears the targetDataTablebefore filling; it does not affect which database the adapter connects to.
References:
AI-generated content may be incorrect. Read our transparency notes for more information. - Ensure the connection string in the
