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 for a list of special folders.

You want either ApplicationDataa, LocalApplicationData (not replicated) or CommonApplicationData, if you really have to keep the database local.

Need Your Help

Count characters in contentEditable DIV

jquery counter contenteditable

I'm using this Plugin to count chars on inputs and textareas:

opencv assertion (general)

c++ opencv

some (most) of opencv errors are discovered in run time - using CV_64 instead of CV_32, accessing out of range, etc. this makes debugging not an easy task, at least when you can't use debug mode from

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.