Connection string with an SQL Server CE database

12,825

Solution 1

In a Win CE application we use the following to get the full path of the executing file:

string StartupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

Using StartupPath you can then add your database name to that path and add it to the connection string:

string datalogicFilePath = Path.Combine(StartupPath, "Datalogic.sdf");
string connectionString = string.Format("DataSource={0}", datalogicFilePath);

Solution 2

You will have to make it work for WinCE, I don't think you have a C:\\Documents and Settings\\ on your target.

I don't really think so because the database works when ...

Look in your App.Config, maybe you already have a connectionstring?

Share:
12,825
rfc1484
Author by

rfc1484

Updated on June 05, 2022

Comments

  • rfc1484
    rfc1484 almost 2 years

    I'm trying to establish a connection with a database in a Windows CE 5.0 application and I'm using the Compact Framework 2.0

    The database is located inside the project's folder:

    C:\Documents and Settings\softdil\My Documents\Visual Studio 2008\Projects\Datalogic\Datalogic

    These are the lines I'm using in order to connect and open the database:

    SqlCeConnection conn = new SqlCeConnection();
    conn.ConnectionString = "Data Source = Datalogic.sdf;";
    conn.Open();
    

    Which gives me a beautiful "database file not found" error message.

    I also tried with the absolute uri with same results:

    conn.ConnectionString = "Data Source = C:\\Documents and Settings\\softdil\\My Documents\\Visual Studio 2008\\Projects\\Datalogic\\Datalogic;";
    

    What am I doing wrong here?

    May be it has something to do with the aplication being debugged (executed) in the mobile device?

    I don't really think so because the database works when the application is loaded, meaning that is associated with a listbox and loads data correctly from the database.

  • rfc1484
    rfc1484 almost 13 years
    Do I have an app.config if I did not created it manually? I do not see it in my solution explorer.
  • Henk Holterman
    Henk Holterman almost 13 years
    No, I forgot but CE does not use them. Still, in your question you say you have data, so what/where is the problem?
  • rfc1484
    rfc1484 almost 13 years
    The problem was that I was trying to connect to the database in my computer but it was searching in the Windows CE terminal and that's why it wasn't found. Anyway it's fixed now.