Question

I'm working on an application that uses a SQL Server CE database. For the final final release of my product I'm going to have my database in a specific location, however, for the testing purposes I need to have it in the same location as the program's executable. The problem with this is my DataDirectory is pointed to the directory where it will be located in the final release. Is there a way to find out what the file location of the executable is and set the DataDirectory to that location?

Update:

I tried the code below, but now it won't even load the data into the DataGridView.

string folder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().CodeBase);
AppDomain.CurrentDomain.SetData("DataDirectory", folder);
Was it helpful?

Solution

string folder= System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().CodeBase);
string dbPath = System.IO.Path.Combine(folder, "DbName.sdf");
string connString = "DataSource=" + dbPath;

Then you can use the connString to open connection to the DB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top