When I try to run my application on an other systems (in my case, windows XP virtualized using VM Virtual Box), I got an error trying to connect to the database (I've copied my release folder on my XP version, this folder contains my sdf database file). My database is a SQL Server Compact database (sdf file).
Here is my code to connect to my database:
if (_Connection == null)
_Connection = new SqlCeConnection(@"Data Source = .\Database.sdf");
It works well when I try on my developpment environment (Windows 8, VS 2012).
The application starts well on my XP machine but it crashes when the connection to the database has to be done.
I tried this approach: Connection string with relative path to the database file
Code :
string ConnectionString = @"Data Source=|DataDirectory|\Database.sdf";
string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
string path = (System.IO.Path.GetDirectoryName(executable));
AppDomain.CurrentDomain.SetData("DataDirectory", path);
_Connection = new SqlCeConnection(ConnectionString);
But when I try to compile in release mode, I have some errors:
Error 1 The database file is not found. Check the path to the database. [ Data Source = C:\Users\Benj\AppData\Local\Microsoft\VisualStudio\11.0\Designer\ShadowCache\1r55aqij.l5f\p2ffjoyu.4ex\Database.sdf ] C:\Users\Benj\Travail\Projets\UNISO\Programme\UNISO V1.0\UNISO\UNISO\Pages\FAQEdit.xaml 14 9 UNISO
The database must be deployed in the application folder.
I've tried this to deploy my application without installing sql server compact: http://msdn.microsoft.com/en-us/library/vstudio/aa983326.aspx
But I don't want to use the Publish Wizard. I just wan to share and run my program by my release folder.
EDIT :
When I install SQL Server Compact on my XP VM, it works well. So the problem is not about the sdf file path. The problem may come from the way to add the SQL Server Compact DLLs to my project. But I've done exactly what it's said in the link above (except the deployment with the wizard).
UPDATE :
As you can see on this link, http://msdn.microsoft.com/en-us/library/vstudio/aa983326.aspx, there are two way to deploy a SQL server Compact application. The first way is to use the traditional Windows installer. I tried it and it works well, my application works perfectly, but I don't want to use this way (I don't want to install things on the client computer, I just want to share a standalone folder). The second way is the Private File–Based Deployment methode (which is the methode that I want to use). I followed all the described instructions in the "Private File-Based Deployment" part in the link above, but when I run the application, it stops when connecting to the database. Is there something I'm missing?