views:

241

answers:

3

I am using a SQL express databases as part of a unit test project in c#.

My databases is located in ./Databases/MyUnitTestDB.mdf

I would like to use a relative path or variable in the app.config rather than having my connection string defined as:

.. AttachDbFilename=C:\blah\blah\blah\yea\yea\yea\MyApplication\Databases\MyUnitTestDB.mdf ..

I have seen the use of |DataDirectory| but am I correct in thinking this is only applicable to web?

I want to control this in the application configuration file, as in production the application uses a hosted sql database.

+1  A: 

I don't have Visual Studio here, but what about:

using System.IO;
using System.Windows.Forms;

string appPath = Path.GetDirectoryName(Application.ExecutablePath);
AttachDBFilme = appPath + "\\MyUnitTestDB.mdf"
Hal
I would suggest something along that line, perhaps used together with ADO.NET's [connection string builders](http://msdn.microsoft.com/en-us/library/ms254947.aspx) to put together the final connection string.
stakx
+2  A: 

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

Dubas
Thanks Dubas, option 1 is what I want to use however I cannot get this working.
Adam Jenkin
Option 1 depends on your executing path. If your UInit test are executing with a different path that your application probably the use of a relative path gives you to a different path that the expected path.
Dubas
A: 

Thanks everyone, I used a combination of your responses.

In my app.config file my connection string is defined as follows

<add name="MyConnectionString"
    connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Database=MyDatabaseForTesting;Trusted_Connection=Yes;" />

In my unit test class I set the DataDirectory property using the following

[TestInitialize]
public void TestInitialize()
{
    AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Databases"));

    // rest of initialize implementation ...
}
Adam Jenkin