当CF应用程序启动时,它执行数据库维护以验证.SDF数据库文件以使用sql Server CE Engine类进行检查:
using (sqlCeEngine engine = new sqlCeEngine(Resources.sqlCeConnectionString)) { Log.Info("Starting database Verification."); if (!engine.Verify()) { Log.Warn("Database Failed verification."); engine.Repair(null,RepairOption.RecoverAllOrFail); Log.Info("Database successfully repaired."); } else { Log.Info("Database Verification successful."); } }
如果应用程序已正确验证,则我将.SDF复制到备份文件夹中:
if (File.Exists(Resources.DatabaseFileLocation)) { File.Delete(Resources.BackupDatabaseFileLocation); File.Copy(Resources.DatabaseFileLocation,Resources.BackupDatabaseFileLocation); Log.Info("Database backup complete."); } else { Log.Info("Could not find Device Database."); }
备份完成后,应用程序启动,应用程序尝试对.SDF进行的第一次连接导致此异常:
There is a file sharing violation. A different process might be using the file.
这是日志文件信息:
2013-05-13 11:52:29,894 [INFO ] - Starting database Verification. 2013-05-13 11:52:33,832 [INFO ] - Database Verification successful. 2013-05-13 11:52:33,838 [INFO ] - Database backup starting. 2013-05-13 11:52:46,941 [INFO ] - Database backup complete. 2013-05-13 11:52:47,933 [ERROR] - There is a file sharing violation. A different process might be using the file. [ \Program Files\ApplicationName\DB.sdf ] at System.Data.sqlServerCe.sqlCeConnection.ProcessResults(Int32 hr) at System.Data.sqlServerCe.sqlCeConnection.Open(Boolean silent) at System.Data.sqlServerCe.sqlCeConnection.Open() at CFApp.MainScreen.GetStartupData() at CFApp.MainScreen..ctor() at CFApp.Program.RunInReleaseMode() at CFApp.Program.Main()
几周以来我一直在研究这个问题的原因,我找不到任何东西.任何指导或帮助将非常感谢.
根据我迄今为止的努力,我可以确认这些事情
>问题是随机发生的.启动过程执行多次,然后它将随机抛出此错误
>考虑到应用程序只是在启动过程中,当时没有与数据库建立任何其他连接.
>每次应用程序打开连接时,它会在完成后立即处理.这是使用using语句完成的.在意外关闭/重启的情况下,没有打开连接.
> sqlCeEngine正在使用using语句进行处理.在处理物体与其连接关闭之间是否存在可能的延迟?
>目前没有其他进程在运行.该设备是
锁定只允许我的应用程序执行.任务
经理只显示ActiveSync和我的应用程序.该设备偶尔会
与另一个桌面应用程序通信
OpenNETCF.Desktop.Communication RAPI库通过USB.活性
必须打开同步才能使其正常工作.该
它与之通信的台式电脑是Win XP.
>我在其他一些论坛上看到ActiveSync可以保留一个
锁定文件可能会导致问题.在ActiveSync中
设备的部分没有选择与之同步
桌面.未选中“文件”复选框.如果其他人有任何
关于如何确保排除此文件的建议
ActiveSync,它也会非常有帮助.
>更新 – 我使用dotPeek来查看sqlCeEngine Dispose方法.我觉得这里和我的一切似乎都井然有序
连接应该妥善处理?
private void Dispose(bool disposing) { if (!disposing) return; this.connStr = (string) null; this.connTokens = (Hashtable) null; NativeMethods.DllRelease(); this.isDisposed = true; }
>更新 – 我尝试运行此测试,以查看是否可以重现错误,我什么也没想到.代码正确执行,没有任何文件共享问题.
for (int i = 0; i < 50; i++) { using (sqlCeEngine engine = new sqlCeEngine(Resources.sqlCeConnectionString)) { //Log.Info("Starting database Verification."); if (!engine.Verify()) { Log.Warn("Database Failed verification."); engine.Repair(null,RepairOption.RecoverAllOrFail); Log.Info("Database successfully repaired."); } else { Log.Info("Verified" + i); } } if (File.Exists(Resources.BackupDatabaseFileLocation)) { File.Delete(Resources.BackupDatabaseFileLocation); } File.Copy(Resources.DatabaseFileLocation,Resources.BackupDatabaseFileLocation); Log.Info("File Copied " + i); GetStartupData(); }
非常感谢您提供的任何信息.