In what format does SqlCeConnection expect its connection string arg?

后端 未结 2 1990
北海茫月
北海茫月 2021-01-24 03:12

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 pre

相关标签:
2条回答
  • 2021-01-24 03:50

    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.

    0 讨论(0)
  • 2021-01-24 03:53

    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.

    0 讨论(0)
提交回复
热议问题