• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

SQL CE access with C# (CF)


I'm developping app on VS.NET2003 with C#, for Small Devices (PocketPC).

I have difficulties to access an SQL CE database (note: SQL CE is correctly installed on device, I've successfully added records to database with Query Analyser.)

Here's the code:
using System.Data.SqlServerCe;

 1            private void Form4_Load(object sender, System.EventArgs e)
 2            {
 3                  if (File.Exists ("pp.sdf"))
 4                  {
 5                        SqlCeConnection      conn = null;
 7                        SqlCeEngine      engine = new SqlCeEngine("Data Source =      pp.sdf");
 8                        engine.CreateDatabase();
10                        try      
11                        {
12                              conn = new SqlCeConnection ("Data Source = pp.sdf");
13                              conn.Open();
15                              SqlCeCommand cmd = conn.CreateCommand();
16                              cmd.CommandText      = "SELECT * FROM employes";
18                              SqlCeDataReader      rdr      = cmd.ExecuteReader();
19                              rdr      = cmd.ExecuteReader();
20                              while (rdr.Read())
21                              {
22                                    lstEmployes.Items.Add(rdr.GetString(1));
23                              }
24                        }
25                        catch (SqlCeException sce)
26                        {
27                              ShowErrors(sce);
28                        }
29                        finally
30                        {
31                              if(conn.State == ConnectionState.Open)
32                                    conn.Close();
33                        }
34                  }
35            }

I took this code from MSDN in Walkthrough sample: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sqlce/htm/_lce_walkthroughs_705.asp?frame=true

I have this error : "An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll -- Additional information: SqlCeException" on line 13.

Any help and/or accurate code sample, tutorials, howtos on the web is welcome.  Thanks to all.

P.S.: I'm disappointed that we now need a premium subscription to see solutions... maybe someone already asked something similar.
2 Solutions
Is het pp.sdf in the correct dir? With "Data Source = pp.sdf" you imply that the data source is in the same dir as your application. During debug, this is your debug dir.
elbaidAuthor Commented:
In fact, I've tried different ways to point to DB file in lines 7 and 12 like ("Data Source = \\Program Files\\AppName\\pp.sdf"), and the same error occurs.  And the if statement checks if file exists.  The file pp.sdf is in the same directory of executable.
hi,. i asume your using vs.net 2003? you should be able to click on the pp.sdf and some sort of progam should launch yes? ok now exit NOT BY the cross button but from the pull down menu.

i see in my code :
pSqlCeConnection = new SqlCeConnection("Data Source = "+ConnectStr);
where ConnectStr = @"\Program Files\appname\pp.sdf"

so this should be ok,... //@"is just to quit escaping chars, handy :) \r\n"

also,.. and this is verry important. THERE CAN BE 1 CONNECTION TO A DATABASE. so make shure no other app is using your db,.. looking trough your code is seems a bit add.. create new database and retrieve data from an unknow table??!!

just prepare 1 db whit the app you have seen when you clicked on the db file. and skip the createdatabase crap. Also ALWAYS use a full path in your connectionstring.

hope this helps

Try this one, I already tested it.

private void Form1_Load(object sender, System.EventArgs e)
if (File.Exists (@"\Program Files\pp.sdf"))
SqlCeConnection conn = null;

conn = new SqlCeConnection("Data Source = \\Program Files\\pp.sdf;ssce:database password='yourpassword'");
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM tableName";
SqlCeDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
catch (SqlCeException sce)
if(conn.State == ConnectionState.Open)

Hope this helps.
Good luck.
elbaidAuthor Commented:
Thank you to everyone, now it works !  Well, almost, now I have to deal with data types !! :)

I hardly need a reference, books or something, maybe ".NET Compact Framework Programming with C#" (3osoft.com/go.php?u=10), but don't know if it covers the SQL Server CE very well.  If someone have a suggestion, I'll appreciate it.

By the way, don't know if someone wants the points, if it's the case, I'll give them to the author of next comment/contribution.  Intervention of both Chintan and God Ares has helped me.  Thank you.

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now