[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


SQL CE access with C# (CF)

Posted on 2004-10-30
Medium Priority
Last Modified: 2008-05-07

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.
Question by:elbaid
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 14

Expert Comment

ID: 12454603
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.

Author Comment

ID: 12454650
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.

Assisted Solution

God_Ares earned 750 total points
ID: 12463010
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


Accepted Solution

chintan_vaishya earned 750 total points
ID: 12472107
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.

Author Comment

ID: 12516826
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question