Working on a windows form application project with 2 developers. Sharing source codes through tfs, have replaced the data directory path by |DataDirectory|
keyword as the data directories are different in each PC. (C:\Users\username\Documents\Visual Studio 20XX\Projects\solution folder\solution folder
)
Have used this relative path in connection string as
class ConnectionManager
{
public static SqlConnection dbcon()
{
string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\systemdb.mdf;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
return con;
}
}
The problem is when the C# application sends data into SQL Server database, it shows that data has been added into the database, but actually the database is not updated.
Found out that the connection string conflicts the .mdf
file located in the root folder with the .mdf
located in bin\debug
. SELECT
queries work fine when using |DataDirectory|
. INSERT query does not work when using |DataDirectory|
, but works fine when using the actual directory path instead of |DataDirectory|
.
How to solve this problem? Requirements are:
Should not need to modify directory paths each time after getting the latest version from tfs, needs something like
|DataDirectory|
to remain always.Connection string should not have a conflict with the
.mdf
files in root folder and thebin\debug
folder.