Problem with connection to SQLCE Database

Last post 07-02-2009 8:26 AM by lspence. 2 replies.

Sort Posts:

  • Problem with connection to SQLCE Database

    06-26-2009, 12:05 AM
    • Star
      8,817 point Star
    • lspence
    • Member since 11-01-2006, 10:12 AM
    • United States
    • Posts 1,358

    I'm having some trouble in understanding why I'm unable to connect to a simple test database I created through code. The database shows under Solution Explorer; however, it throws a SqlCeException with "The database file cannot be found as soon as it tries to open the connection. Check the path to the database. [Data Source = test.sdf]". If I navigate to the projects location using Explorer I can clearly see the database file. Below is my code for testing this.

    string connectionString = "Data Source=test.sdf;password=password;encrypt=true";
    SqlCeEngine engine = new SqlCeEngine(connectionString);
    engine.CreateDatabase();

    SqlCeConnection conn = new SqlCeConnection(connectionString);
    SqlCeCommand cmd = new SqlCeCommand();

    cmd.CommandText = "SELECT * FROM Project";
    conn.Open();
    SqlCeResultSet resultSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable |
    ResultSetOptions.Updatable);
    this.bindingSource1.DataSource = resultSet;


    Any ideas why it is unable to find the database when it is actually there within the project?


    My Blog
    http://Lspence.blogspot.com

    (Please MARK this post as ANSWERED, if you find it helpful)
  • Re: Problem with connection to SQLCE Database

    07-02-2009, 6:06 AM
    Answer

     Hi lspence,

    There is no relative path concept in WinCE platform, so it is obviouse that your path is wrong,  we have to take absolute path, like the code below:

    ConnectionString = "Data Source = \\Program Files\\DeviceApplication6\\data.sdf";

     

    Best regards,
    Guang-Ming Bian - MSFT

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Problem with connection to SQLCE Database

    07-02-2009, 8:26 AM
    • Star
      8,817 point Star
    • lspence
    • Member since 11-01-2006, 10:12 AM
    • United States
    • Posts 1,358

    Thanks, I already figured it out after doing some more research.

    My Blog
    http://Lspence.blogspot.com

    (Please MARK this post as ANSWERED, if you find it helpful)
Page 1 of 1 (3 items)