0

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?

Community
  • 1
  • 1
Ben
  • 3,972
  • 8
  • 43
  • 82
  • What's the error message you get after applying the change I suggested in my answer? – Thorsten Dittmar Sep 20 '13 at 10:41
  • I think the probleme is no longer about the database path. It works well when SQL Server Compact is installed on the client PC. That mean that the database is well found (because I can read and write on the database). I use this code to connect to the database : _Connection = new SqlCeConnection("Data Source = Database.sdf"); It works well. The problem is only about the Private File-Based Deployment method which doesn't work (as I said in the update part of my post). – Ben Sep 20 '13 at 12:27

1 Answers1

0

Well, if the database file resides in your application's folder, why don't you just do this:

string appFolder = Path.GetDirectory(Assembly.GetEntryAssembly().Location);
string dbFile = Path.Combine(appFolder, "Database.sdf");
string ConnectionString = String.Format(@"Data Source={0}", dbFile);

By the way: Sql Server Compact is meant to be used without installation. It is actually not a server, but a set of DLLs that manage the database.

.\Database.sdf may not work because .\ does not resolve relative to the folder the application is in, but relative to the current directory, which may be different! It is important to keep that in mind.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • I have a lot of xaml errors when I try with your solution (only in Release, I have no errors in Debug). But the problem seems to be elsewhere. Please have a look to my edits. – Ben Sep 19 '13 at 13:10
  • On the Microsoft "How to deploy..." page you linked above, it says exactly what to do in the "Private file-based deployment". You may not have done that correctly. All seven DLLs need to be added to your solutions and all seven DLLs need to be in the Bin\Release directory, and all of them need to be copied to the client. Actually I have no idea why you should get XAML errors - none of what I've said has to do with XAML. – Thorsten Dittmar Sep 19 '13 at 13:49
  • All seven DLLs are added to my solution and all seven DLLs are in the Bin\Release directory, and all of them are copied to the client. I've done the manipulation many times but I always get the same result. About my XAML errors, this is really a mystery. – Ben Sep 19 '13 at 16:13
  • So, if I understand you correctly, you don't get an exception about the database anymore, but now you get XAML errors? I bet this has something to do with binding problems. But that aside, I suggest you simply create a test solution where you add the required DLLs and the DB file and simply try to open the database and select something. This can be a console application, too. Just to check you're doing it right. If you can deploy this successfully, the error must be something else. – Thorsten Dittmar Sep 20 '13 at 06:50
  • No, my problem is no longer the XAML errors. Have a look to the "Edit" part of my post. – Ben Sep 20 '13 at 08:41
  • So after that, what exactly *is* your problem? – Thorsten Dittmar Sep 20 '13 at 08:53
  • What's the error message after you applied the change I suggested in my answer? – Thorsten Dittmar Sep 20 '13 at 10:41
  • I think the probleme is no longer about the database path. It works well when SQL Server Compact is installed on the client PC. That mean that the database is well found (because I can read and write on the database). I use this code to connect to the database : _Connection = new SqlCeConnection("Data Source = Database.sdf"); It works well. The problem is only about the Private File-Based Deployment method which doesn't work (as I said in the update part of my post). – Ben Sep 20 '13 at 12:26
  • Yes, I understand that. But it is simply not possible that you get the same error message using my answer that you get without applying my changes. So what's the error message? – Thorsten Dittmar Sep 20 '13 at 12:42
  • Ok, I just re-try your solution and I don't know how but I have no error now. – Ben Sep 20 '13 at 14:23