LinqToSQL 中的连接字符串
我用 C# 编写了一个示例,但是当我使用相对连接字符串时,它不会将数据保存到数据库,甚至不会出现任何异常或错误:
private void AddButton_Click(object sender, EventArgs e)
{
using (DataBaseModelDataContext DB = new DataBaseModelDataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DataBase.mdf;Integrated Security=True;User Instance=True"))
{
Person person = new Person { Name=NameField.Text,LastName=LastNameField.Text};
DB.Persons.InsertOnSubmit(person);
DB.SubmitChanges();
MessageBox.Show("Add successfully");
}
}
但是当我更改连接字符串时它可以工作:
using (DataBaseDataContext DB = new DataBaseDataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Herald\Documents\Visual Studio 2010\Projects\LinqToSql\LinqToSql\DataBase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
但我需要使用相对连接字符串我应该做什么:
i wrote a sample with c# but when i use relative connectionstring it doesnt save data to database and doesnt make any exception or error even:
private void AddButton_Click(object sender, EventArgs e)
{
using (DataBaseModelDataContext DB = new DataBaseModelDataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DataBase.mdf;Integrated Security=True;User Instance=True"))
{
Person person = new Person { Name=NameField.Text,LastName=LastNameField.Text};
DB.Persons.InsertOnSubmit(person);
DB.SubmitChanges();
MessageBox.Show("Add successfully");
}
}
but when i change connection string it works:
using (DataBaseDataContext DB = new DataBaseDataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Herald\Documents\Visual Studio 2010\Projects\LinqToSql\LinqToSql\DataBase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
but i need to use relative connection string what should i do:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要获取当前目录并将其映射到字符串中。
You'll need to get the current directory and map it into the string.