如果我在应用程序运行时更改Microsoft SQL Server Management Studio中的数据,LINQ将无法获取新数据,即使我运行
myContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues)
获取新更改的唯一方法(我到目前为止已找到...)是重新创建数据上下文。
myContext.Dispose();
myContext = new MyDataContext();
我必须是盲目的,这必须非常明显,但我浪费了足够的时间来发表这个愚蠢的问题... -_-''
谢谢!
修改
从SQL加载数据时:
myBindingSource.DataSource = myContext.myTable.Where(o => o.id != 0).OrderBy(o => o.name);
答案 0 :(得分:0)
我的概念存在缺陷,并且DbContext不应该比您需要执行的查询更长。也就是说,如果我想刷新我的数据,我应该得到一个新的DbContext实例并重新填充我的数据源。
答案 1 :(得分:-1)
让connectiontrin为:
string pp = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\AdventureWorks2012_Data.mdf;Integrated Security=True;Connect Timeout=30";
并更新任务如下:
public async Task callupdate()
{
try
{
int ppp = Convert.ToInt32(textBox1ID.Text);
DataClasses1DataContext dc = new DataClasses1DataContext(pp);
Person person = dc.Persons.Single(c => c.BusinessEntityID == ppp);
person.PersonType = Convert.ToString(PersonTypecomboBox1.SelectedItem);
person.PersonType = Convert.ToString(PersonTypecomboBox1.SelectedItem);
if (NameStylecomboBox1.SelectedText == "False")
person.NameStyle = false;
else
person.NameStyle = true;
person.Title = Convert.ToString(TitlecomboBox1.SelectedItem);
person.FirstName = FirstNametextBox2.Text;
person.MiddleName = MiddleNametextBox3.Text;
person.LastName = LastNametextBox4.Text;
person.Suffix = SuffixtextBox5.Text;
person.EmailPromotion = Convert.ToInt32(EmailPromotiontextBox6.Text);
person.ModifiedDate = DateTime.Today;
dc.SubmitChanges();
}
catch(Exception exp)
{
}
}
而不是DataClasses1DataContext dc = new DataClasses1DataContext();
DataClasses1DataContext dc = new DataClasses1DataContext(pp);
Vby调用SubmitChanges()
作为我们类对象的更新数据实际上是在实际数据库中编写的