Tuesday 15 June 2010

c# - Connection string with relative path to the database file -


I am using the full path to the database file I load the data from the sdf database in the winforms app.

  conn = new SqlCeConnection {ConnectionString = "Data Source = F: \\ My Documents \\ Project1 \\ bin \\ Debug \ Database.sdf"};  

I want to use a relative path in the database file. for example. I have an SDF file in the folder F: \ My Document \ Project 1 \ bin \ debug \ data \ file.sdf and I want to use relative path in the connection string. Any suggestions ? Thank you.

relative path:

  ConnectionString = "data source = | datadirectory | \ Database.sdf ";  

Modifying the data directory as an executable path:

  string executable = system. reflection. Assembly. String path = (System.IO.Path.GetDirectoryName (executable)); AppDomain.CurrentDomain.SetData ("DataDirectory", path);  

No comments:

Post a Comment