Data source configuration wizard sets absolute path of the database
Hello,
I am using VS2008 and access database.
I keep my database from application data folder. When I try to run the program in a different computer, I see in the error message, absolute path of the database file even though I used relative path. When I search the path as a string in the entire solution I found it in three files:
Settings.Designer.vb
Settings.settings (xml code)
app.config (xml code)
Is there an easy way to solve this problem or should I edit the files manually?
Re: Data source configuration wizard sets absolute path of the database
If you want to specify the program folder in a connection string then you use |DataDirectory|. If you want to change a connection string after deployment then you change it in the config file.
Re: Data source configuration wizard sets absolute path of the database
This is how I define my connection string in the code.
Code:
Dim appDataFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
'connection string
Dim cnStr As String = "Driver={Microsoft Access Driver (*.mdb)};" & _
"DBQ=licence_manager_2003.mdb;" & _
"DefaultDir=" & appDataFolder & "\MyDataFolder;" & _
"Uid=Admin;Pwd=;"
I also changed settings.designer
Code:
<Global.System.Configuration.ApplicationScopedSettingAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Configuration.SpecialSettingAttribute(Global.System.Configuration.SpecialSetting.ConnectionString), _
Global.System.Configuration.DefaultSettingValueAttribute("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""|DataDirectory|\MyDataFolder" & _
"\licence_manager_2003.mdb""")>
I also set |DataDirectory| folder while the main form loads:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AppDomain.CurrentDomain.SetData("DataDirectory", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
However it is pointing the debugging directory and I get "not a valid path" error.
What is wrong here? I am a beginner, what is the proper way to do that? Could you please explain in detail?
Re: Data source configuration wizard sets absolute path of the database
There's no point setting the DataDirectory if you're not using |DataDirectory| in your connection string. This:
Code:
Dim cnStr As String = "Driver={Microsoft Access Driver (*.mdb)};" & _
"DBQ=licence_manager_2003.mdb;" & _
"DefaultDir=|DataDirectory|\MyDataFolder;" & _
"Uid=Admin;Pwd=;"
Also, don't use ODBC unless you have to. For Access, use OLE DB.