这是我的课:WP7 - 访问数据库文件是不允许
[Table]
public class Question
{
[Column]
public bool Sort { get; set; }
[Column(IsPrimaryKey = true)]
public int QuestionID { get; set; }
[Column]
public int Level { get; set; }
[Column]
public string Description { get; set; }
[Column]
public string Answer1 { get; set; }
[Column]
public string Answer2 { get; set; }
[Column]
public string Answer3 { get; set; }
[Column]
public string Answer4 { get; set; }
[Column]
public string RightAnswer { get; set; }
[Column]
public bool Show { get; set; }
}
public class QuestionContext : DataContext
{
public QuestionContext(string connectionString)
: base(connectionString)
{
}
public Table<Question> Questions
{
get
{
return this.GetTable<Question>();
}
}
}
我尝试连接到现有数据库:
private const string ConnectionString = @"appdata:App_Data\QuestionDb.sdf";
public GamePage()
{
InitializeComponent();
using (QuestionContext context = new QuestionContext(ConnectionString))
{
if (!context.DatabaseExists())
{
// create database if it does not exist
context.CreateDatabase();
}
else
{
var questions = from o in context.Questions where o.Level == 1 && o.Show == false select o;
var question = questions.FirstOrDefault();
}
}
}
我得到的错误与“访问数据库文件不被允许“。我认为问题出在我的连接字符串中。它出什么问题了?我如何才能访问文件夹App_Data中的SQLCE数据库?
我会尝试将数据库文件移动到另一个位置,例如。在Windows 7上,如果您尝试访问位于程序文件内的数据库文件(它只是为管理员用户保护),它会给出相同的错误 – erem
我看到了这一点,但我认为这不会是我的问题。我将我的数据库添加到了我的解决方案中,所以现在我已将它放在我有项目的位置,并且它不在Program Files或具有类似限制的其他位置。 –