2014-03-06 80 views
2

在我的Windows Phone 8 C#/ XAML .NET 4.5項目中,我正在使用(在LINQ-2-SQL的幫助下)由團隊的另一位成員創建的本地數據庫(可能是SQLite)。無法找到數據庫文件

但是當我運行應用程序,它拋出一個異常:

The database file cannot be found. Check the path to the database. [ Data Source = C:\Data\Users\DefApps\AppData\{XXXXX-XXXXX-XXXXXX-XXXXX}\Local\database.sdf ] 

.sdf文件與數據庫是在/Database/database.sdf項目和屬性都設置爲Build action: Embedded resource & Copy to output directory: Copy always

databaseContext.cs,這是用作上下文類(不知道我的命名是正確的,我是新來的LINQ-2-SQL),連接字符串被定義爲:

Data Source=isostore:/database.sdf 

這是正確的設置?我能做些什麼來使它工作?

+0

我還沒有玩過這個,但也許你需要首先將文件從ResourceStream複製到IsolatedStorage。類似於[此鏈接](http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202978(v = vs.105).aspx#BKMK_HowtoPlayBackgroundAudio)中的CopyToIsolatedStorage()。這只是一個猜測,但也許會有所幫助。 – Romasz

回答

1

您必須先將數據庫移至獨立存儲。繼承人如何做到這一點。

public static void MoveReferenceDatabase() 
    { 
     // Obtain the virtual store for the application. 
     IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); 

     // Create a stream for the file in the installation folder. 
     using (Stream input = Application.GetResourceStream(new Uri("DutchMe.sdf", UriKind.Relative)).Stream) 
     { 
      // Create a stream for the new file in the local folder. 
      using (IsolatedStorageFileStream output = iso.CreateFile("DutchMe.sdf")) 
      { 
       // Initialize the buffer. 
       byte[] readBuffer = new byte[4096]; 
       int bytesRead = -1; 

       // Copy the file from the installation folder to the local folder. 
       while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0) 
       { 
        output.Write(readBuffer, 0, bytesRead); 
       } 
      } 
     } 
    } 

否則 連接字符串設置爲數據源= APPDATA:/database.sdf。我對第二種解決方案不太確定。