Question

I have a call to SqlCeConnection.Open() that's failing. The exception message I'm getting is pretty generic: "System.Data.SqlserverCe.SqlCeException" (more generic than pretty)

The pertinent code is:

private DBConnection()
{
    try
    {
        string conStr = "Data Source = " + filename;
        string cmpStr = conStr + ".tmp";
        if (File.Exists(filename+".tmp"))
            File.Delete(filename+".tmp");

        engine = new SqlCeEngine(conStr);

        if (File.Exists(filename))
        {
        }
        else
        {
            engine.CreateDatabase();

            }
        engine.Dispose();

        objCon = new SqlCeConnection(conStr);                                                                           
            //MessageBox.Show(string.Format("conStr == {0}", conStr)); 
        objCon.Open(); // <= This is where all Dallas breaks loose; conStr is "DataSource = \My Documents\NRPSDB.SDF" (which file *does* exist on the device)
    }
    catch(Exception ex)
    {
        NRPS.ExceptionHandler(ex, "DBConnection.DBConnection");
    }
}

Is the problem the connection string arg passed to the SqlCeConnection constructor? Is the "DataSource =" preamble unnecessary/problematic? Do the backwhacks need to be doubled or assigned to a verbatim string? Or...???

For more info, see this scintillating exchange:

UPDATE

Grant Winney's answer looked promising, but trying it today, I find that it doesn't compile in my (Windows CE / CF) situation. The possibilities for me following "SpecialFolder" are:

ApplicationsData
Favorites
Personal
Programs
StartMenu
Startup

None of these correspond to "MyDocuments" do they?

UPDATE 2

Expanded response to ctacke's comment below:

Here is the place where the SqlCe exception gets embedded within a MessageBox.Show() "debug" string:

public static string GetFormTitle(string formName, string serialNo, string siteNo)
{
    MessageBox.Show(string.Format("GetFormTitle() reached. formName == {0}; serialNo == {1}; siteNo == {2}", formName, serialNo, siteNo)); // TODO: Remove after testing
    string titleBar = formName == "" ? "NRPS HHS" : formName;

What I end up seeing is:

"GetFormTitle() reached. formName == NRPS: System.Data.SqlServerCe.SqlCeException; serialNo == ; siteNo == ;"

A related post/thread is here

UPDATE 3

The bizarro custom exception handler does attempt to show exception detail like so:

public static void ExceptionHandler(Exception ex, string location)
{
    try
    {
        MessageBox.Show("Exception: " + ex.Message + "\n\nLocation: " + location, GetFormTitle("NRPS: " + ex.GetType().FullName,"",""));
    }
    catch(Exception exc)
    {
        MessageBox.Show("Exception Handler generated an exception!\n" + exc.Message + "\n\nCalling Location: " + location, GetFormTitle("NRPS: " + exc.GetType().FullName,"",""));
    }
}

UPDATE 4

Now we're getting somewhere maybe. I commented out the existing line in the custom exception handler that called GetFormTitle() and replaced it with this:

MessageBox.Show(string.Format("location: {0}; Exception: {1}; Inner Exception: {2}; TypeFullName: {3}; StackTrace: {4}" + location, ex.Message, ex.InnerException, ex.GetType().FullName, ex.StackTrace));

This is what I see now:

"location: DBConnection.DBConnection:

Exception: Incompatible Database Version. If this was a compatible file, run repair. For other cases refer to documentation. [DB version = 0,Requested version = 0,File name = My Documents\HHSDB.SDF ];

Inner Exception: TypeFullName: System.Data.SqlServerCe.SqlCeException;

StackTrace: at System.Data.SqlServerCe.SqlCeConnection.ProcessResults(Int 32 hr) at System.Data.SqlServerCe.SqlCeConnection.Open(boolean silent) at System.Data.SqlServerCe.SqlCeConn... [can't see any more...]*"

This is what strikes me: DB version = 0,Requested version = 0

But...what the Hec Ramsey?!?

UPDATE 5

This has morped into a new question, which I posted here

UPDATE 6

And that one to another, which is here

Was it helpful?

Solution

The problem, as indicated by the error text, is that the SDF file was created by a version of SQL Compact that doesn't match the version of SQL Compact that the application is referencing. I wouldn't focus on the reported version numbers, just the fact that it knows there's a mismatch.

If you don't know the version of the SDF, you can always look it up by reading a few bytes from the SDF file.

SQL Compact database files aren't 100% transportable. You definitely cannot take an SDF from a newer version and load it with an older set of runtimes.

If the database was created with 3.1, you can upgrade it to 3.5 by calling SqlCeEngine.Upgrade().

You cannot programmatically upgrade from 1.0 or 2.0 to any newer version. You must use the older SQL Compact libraries to interact with the database or you must recreate the database targeting the newer runtimes.

If the database was created on a PC, the first use will force a full re-index because the indexes are formatted differently on a device. If you have a lot of data in the database, it's often best to make sure what you deploy to the device was actually last opened on a device to prevent users from having to wait for that re-index.

OTHER TIPS

Since filename contains the relative path "\My Documents\NRPSDB.SDF", your program may be looking for that path starting at the directory your application is running from, which is most likely the wrong place. Something like:

... \bin\debug\My Documents\NRPSDB.SDF

Try placing this in your method and see if it's able to find the file:

string conStr = string.Concat("Data Source = ",
   Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                "NRPSDB.SDF"));

If it is able to find it, then you'll have to use this wherever you're calling the method from, so that filename contains the correct absolute path.


Regarding your update that you're using Win CE, the equivalent to SpecialFolder.MyDocuments is SpecialFolder.Personal.

From MSDN:

Personal. The directory that serves as a common repository for documents. This member is equivalent to MyDocuments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top