I’m writing an application in C# and I am using a SQL database too keep some information. I have defined the database address to make a connection like this:
@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\pouya\documents\visual studio 2010\Projects\DataBaseDLL\DataBaseDLL\Database.mdf
In this case when I run the application on another computer it causes an error because the address is incorrect.
Is there any way that I can fix this error? For example, making the application look for it in the same folder like ..\\database.mdf?
You should use a relative path to your *.mdf file.
The following connectionString looks for the mdf file within your App_Data folder. Put this connectionString into your web.config:
The
|DataDirectory|is doing the trick…