2014-03-06 58 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。我对第二种解决方案不太确定。