在我的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 :(得分: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);
}
}
}
}
否则 将连接字符串设置为Data Source = appdata:/database.sdf。我不太确定第二种解决方案。