2012-11-05 123 views
-3
using (EntityDataContext amdb = new EntityDataContext(StrConnectionString)) 
      { 
       if (amdb.DatabaseExists()) 
       { 
        using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 
        { 
         if (!isoStore.FileExists(databaseName)) 
         { 
          copyDatabase = true; 
         } 
         else 
         { 
          using (IsolatedStorageFileStream databaseStream = isoStore.OpenFile(databaseName, FileMode.Open, FileAccess.Read)) // error here 
          { 
           using (Stream db = Application.GetResourceStream(new Uri(databaseName, UriKind.Relative)).Stream) 
           { 
            if (databaseStream.Length < db.Length) 
             copyDatabase = true; 
           } 
          } 
         } 
        } 
       } 
       else 
       { 
        //error with the database that has been packaged 
       } 
       if (copyDatabase) 
        new Worker().Copy(databaseName); 
      } 
+0

異常是問題的標題, – 1Mayur

+0

Eror發生在第一個t ime模擬器運行,之後,它運行良好,直到我關閉它 – 1Mayur

+0

你應該包括這個問題本身的信息,而不只是發佈你的代碼。 **解釋**代碼試圖做什麼,出了什麼問題。 – ChrisF

回答

0

檢查您是否確切地在獨立存儲訪問模式參數中寫入數據的可能性,而不是僅僅能夠讀取。

你用設備測試過嗎?

+0

第一次模擬器運行時發生錯誤,之後,我工作正常,直到我關閉它 – 1Mayur

0

從我可以告訴你試圖讀取數據庫文件,而你仍然打開數據庫連接。由於DataContext會鎖定數據庫(以及文件),因此不允許同時讀取它。

爲了關閉數據庫連接嘗試關閉EntityDataContext對象(通過調用amdb.Close()或關閉using語句

嘗試是這樣的:

bool shouldCopyDatabase = false; 
bool databaseExists = false; 

using (EntityDataContext amdb = new EntityDataContext(StrConnectionString)) 
{ 
    databaseExists = amdb.DatabaseExists(); 
} 

if (databaseExists == true) 
{ 
    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
     if (!isoStore.FileExists(databaseName)) 
     { 
      copyDatabase = true; 
     } 
     else 
     { 
      using (IsolatedStorageFileStream databaseStream = isoStore.OpenFile(databaseName, FileMode.Open, FileAccess.Read)) // error here 
      { 
       using (Stream db = Application.GetResourceStream(new Uri(databaseName, UriKind.Relative)).Stream) 
       { 
        if (databaseStream.Length < db.Length) 
         copyDatabase = true; 
       } 
      } 
     } 
    } 
} 

if (copyDatabase) 
    new Worker().Copy(databaseName); 

通過移動獨立存儲訪問功能在using (EntityDataContext amdb = new EntityDataContext(StrConnectionString))範圍之外,您允許首先關閉數據庫連接。

相關問題