root database location
I don't know where to place the db so that when i will have my final app .exe, in it's folder i would have another data folder and in that folder the db.mdf. Here is my connection String :
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Users\\alex\\Documents\\Visual Studio 2010\\Projects\\Network_Remote_Monitoring\\Network_Remote_Monitoring\\data\\bd.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True";
(i.e. i would like a relative refference to the db, something like AttachDbFilename=".\data\bd.mdf" )
does the relative path data\\bd.mdf not work in connection stings? e.g.,
con.ConnectionString = "DataSource=.\\SQLEXPRESS;AttachDbFilename='data\\bd.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True";
Otherwise you could use the current directory shortcut e.g., something like
string dbdirectory = System.IO.Directory.GetCurrentDirectory() + "\\data\\bd.mdf" con.ConnectionString = "DataSource=.\\SQLEXPRESS;AttachDbFilename='" + dbdirectory + "';Integrated Security=True;Connect Timeout=30;User Instance=True";
Or you could even open a file dialog and ask the user where they want to store the program data, and grab the full path from there.
EDIT: This is how you would achieve what you want, I would however suggest storing the db somewhere else like @TomTom suggested
that when i will have my final app .exe, in it's folder i would have another data folder and in that folder the db.mdf
DO NOT DO THAT. Databases should never live:
- In the programs folder
- On a network drive (starting with \).
Why attach it like that anyway? In most cases (all I ever worked on) databases are explicitly crated on a server. Local databases should live either in the users program data (NOT DOCUMENTS!) folder or the shared one.
Check http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx for a list of special folders.
You want either ApplicationDataa, LocalApplicationData (not replicated) or CommonApplicationData, if you really have to keep the database local.