C# 找不到数据库文件
在我的WindowsPhone8c#/XAML.NET4.5项目中,我正在使用(在LINQ-2-SQL的帮助下)一个本地数据库(可能是SQLite),该数据库由团队的另一名成员创建 但当我运行应用程序时,它会抛出一个异常:C# 找不到数据库文件,c#,linq-to-sql,windows-phone-8,C#,Linq To Sql,Windows Phone 8,在我的WindowsPhone8c#/XAML.NET4.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
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
中的项目中,属性设置为生成操作:嵌入式资源&复制到输出目录:始终复制
在databaseContext.cs
中,连接字符串定义为:
Data Source=isostore:/database.sdf
这是正确的设置吗?我该怎么做才能使它工作?您必须首先将数据库移动到独立存储。这是你能做到的
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。我对第二种解决方案不是很确定。我还没有玩过这个,但可能您需要先将文件从ResourceStream复制到IsolatedStorage。类似于
CopyToIsolatedStorage()
at。这只是一个猜测,但可能会有所帮助。