Tuesday, September 16, 2008

SqlCe Connection String

I am leaving this post for the generation of Windows Mobile programmers, who like me spent over 45 minutes trying to find how to get the connection string work for SQLCE 3.5 using the .Net Compact Framework.

I don't know why this information is so cryptic, or why I felt like Indiana Jones when I uncovered the truth!
Of Course its so simple!
Why didn't I find this sooner?
How come this wasn't in the first line of documentation?
If you are reading this post with earnest and eager anticipation like a child ready to devour an unopened present, then you will be saying the same thing in several minutes. Read on, read on.

Basically, unless you do some digging you will find that Microsoft did something less than obvious when setting up Database Connections using Visual Studio and a Mobile Device. You would expect to be able to use relative paths for everything right? It makes sense that if you create something in a given project, then to reference it you would use a relative path right? Well, you are clearly wrong! You have to use different paths depending on whether you are developing or deploying! Don't ask me why but you do!

I will not try and re-invent the wheel and give you all the details on how to set up the connection using Visual Studio since Sam Allen has done such a job of doing so here, http://dotnetperls.com/Content/SQLCE-Database-Use.aspx.

I ran into a few problems since Sam made a few assumptions that didn't work out for me and might cause some problems for others as well. He suggests using the system properties object like so,
Properties.Settings.Default.datanameConnectionString;

However, this doesn't always work depending on how you have your environment configured and when starting a Smart Device Project, you might not have access to this.

So really what you are looking for is something like this,
string conString = @"C:\work\projects\vsproject\database.sdf";
using (SqlCeConnection con = new SqlCeConnection(conString))
{
con.Open();
}

Problem is, Visual Studio assumes you know that all of the files for your Smart Device Project are copied to the Device you are deploying to. The default location is in the Program Files directory of your device. Okay, this sounds easy enough! Yet, if you read the documentation you will see this sprinkled around,
string conString = @"My Device\Program Files\SmartDeviceProject\database.sdf";

This does not work! It might have worked with .Net 2.0 or Visual Studio 2005, but it doesn't work with the newest software. Good news is that there is an easy fix! All you have to do is remember that Mobile Devices have a root directory '\'

So here it is! Are you ready? It's been a long journey but I was happy to go along with you.

SQLCE 3.5 Connection String for a Windows Mobile Device Project is...

string conString = @"\Program Files\SmartDeviceProject\database.sdf";


Where SmartDeviceProject is the name of your Project and database.sdf is the name of your database!

I hope this helps!

Good night,

Kent

No comments: