[英]WP7 - Access to the database file is not allowed
This is my class: 这是我的课:
[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>();
}
}
}
I am trying to connect to my existing database: 我正在尝试连接到现有数据库:
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();
}
}
}
I get error with "Access to the database file is not allowed". 我收到“不允许访问数据库文件”错误。 I think problem is in my connection string.
我认为问题出在我的连接字符串中。 What's wrong with it?
它出什么问题了? How can I get access to SQLCE database in folder App_Data?
如何访问文件夹App_Data中的SQLCE数据库?
In your app.config add the following 在您的app.config中添加以下内容
<connectionStrings>
<add name="DataContext"
connectionString="Data source=DataDirectory|Database.sdf;"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
Then get the connectionstring with help from ConfigurationManager. 然后在ConfigurationManager的帮助下获取连接字符串。 Maybe you need to add a reference to System.Configuration.
也许您需要添加对System.Configuration的引用。
private const string ConnectionString = ConfigurationManager.ConnectionStrings["DataContext"].ConnectionString
You can learn more about DataDirectory in ConnectionString here: http://msdn.microsoft.com/en-us/library/cc716756(v=vs.100).aspx 您可以在这里在ConnectionString中了解有关DataDirectory的更多信息: http : //msdn.microsoft.com/zh-cn/library/cc716756( v=vs.100) .aspx
You must compose the connection string like this: 您必须像这样编写连接字符串:
MyDataContext db = new MyDataContext("Data Source = 'appdata:/mydb.sdf'; File Mode = read only;");
The database file will be read only... 数据库文件将是只读的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.