I am working on a project and it is a Windows based .NET application. I want to make a relative path so that when I move my files to another computer, I won't set up a new connection again.
So far, here's what I have done.
In my app.config
:
<connectionStrings>
<add name="ConString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\dbOffense.mdf;Integrated Security=True;Connect Timeout=30"
providerName="System.Data.SqlClient" />
</connectionStrings>
In my program. The code is not in any function. It is globally declared.
Dim constr As String=ConfigurationManager.ConnectionStrings("ConString").ConnectionString()
Dim con As New SqlConnection(constr)
My database dbOffense.mdf
is located in the same folder as my Windows application OffenseDatabase
. It is not in the folder where the .exe
file of my application is.
So far, all that I've read doesn't really work at all.
I have tried moving my dbOffense.mdf
to bin/Debug/Database
directory. Then whenever I remove and set up a new connection/data source in my .NET application, the dataset is generated in the same folder with my Windows application OffenseDatabase
instead of the same folder where dbOffense.mdf
is.
Please can someone help?