Yes, |DataDirectory| is only for Web application to select the App_Data directory of the web application.
You have two posiblities to create the connection:
1.- Use a relative path:
String con ="... AttachDbFilename=Databases\MyUnitTestDB.mdf ... ";
2.- get the application path and add to the String.
In c# Windows Application you can use Application.StartupPath
String con= " ... AttachDbFilename=" + Application.StartupPath + "\Databases\MyUnitTestDB.mdf ... ";
Depending on the applicaiton type or launch mode you got different properties. Ex:
- Application.StartupPath -- The start path of the exe application that starts the application
Application.ExecutablePath -- the start path an name of the exe application that stats the application
But to use Application you need to include System.Windows.Forms that is not included for example into console applications.
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) -- This gets the path from the current assembly "dll,exe,..." Is not affected by application type, path changes,... Always return the directory when the Assemby resides.
Environment.CurrentDirectory -- the current directory. This can be changed for example if you navigate into folders.
You can find more about the different connection string options here
http://www.connectionstrings.com/sql-server-2005