1

I have the following code, just to test connection:

public void Test()
{
    SqlCeConnection conn = new SqlCeConnection(@"Data Source=/Application/Database.sdf;");
    try
    {
        conn.Open();
        label1.text = "Connection!";
    }
    catch (Exception ee)
    {
        label1.text = "No connection!";
    }
}

When trying to connect to this database, the application throws an exception at conn.Open() saying

SqlCeException was unhandled

and nothing more. The exception message is blank, so I'm having a hard time figuring out what went wrong.

The database file is there, and the application returns true with

File.Exist(@"/Application/Database.sdf");

so it does have access to the file.

I'm probably doing something really wrong here, can anyone help me out with this?

I'm using Compact Framework 2.0 on Windows CE 5, and the application in question is an existing one. I'm trying to add a database to it so I can load large amounts of data much more easier.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    Start by implementing proper error handling: http://msdn.microsoft.com/da-dk/library/ms174079(v=sql.100).aspx – ErikEJ May 21 '13 at 13:33
  • 2
    If you enter the full path does it work? Also, should your `/` be `\\`? – Belogix May 21 '13 at 13:38
  • I would also just double-check you can connect to it in SSMS (or your favourite database management tool) fine. – Arran May 21 '13 at 13:46
  • @ErikEJ I know, this is just something I wrote real quick - It's not for production purposes. –  May 21 '13 at 13:49
  • @Belogix This is the full path, CE has those two switched around for some reason. –  May 21 '13 at 13:50
  • @Arran I can access the database fine using VS2008 if that makes any importance –  May 21 '13 at 13:51
  • But proper error handling might give you a hint to what the error is... – ErikEJ May 21 '13 at 14:20

1 Answers1

1

What Erik is saying is change your code to this:

public void Test()
{
  SqlCeConnection conn = new SqlCeConnection(@"Data Source=/Application/Database.sdf;");
  try
  {
    conn.Open();
    label1.text = "Connection!";
  }
  catch (SqlCeException ee)  // <- Notice the use of SqlCeException to read your errors
  {
    SqlCeErrorCollection errorCollection = ee.Errors;

    StringBuilder bld = new StringBuilder();
    Exception inner = ee.InnerException;

    if (null != inner) 
    {
      MessageBox.Show("Inner Exception: " + inner.ToString());
    }
    // Enumerate the errors to a message box.
    foreach (SqlCeError err in errorCollection) 
    {
      bld.Append("\n Error Code: " + err.HResult.ToString("X")); 
      bld.Append("\n Message   : " + err.Message);
      bld.Append("\n Minor Err.: " + err.NativeError);
      bld.Append("\n Source    : " + err.Source);

      // Enumerate each numeric parameter for the error.
      foreach (int numPar in err.NumericErrorParameters) 
      {
        if (0 != numPar) bld.Append("\n Num. Par. : " + numPar);
      }

      // Enumerate each string parameter for the error.
      foreach (string errPar in err.ErrorParameters) 
      {
        if (String.Empty != errPar) bld.Append("\n Err. Par. : " + errPar);
      }

    }
    label1.text = bld.ToString();
    bld.Remove(0, bld.Length);
  }
}

The generic Exception you are catching right now can not give you the details of the SqlCeException.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862