使用Linq to sql概念在wpf的更新记录中出现问题

时间:2012-07-02 09:05:17

标签: c# wpf linq-to-sql

        MyDatatBaseDataContext MyDB = new MyDatatBaseDataContext();
        var _Update = from u in MyDB.Employees where u.Address == "WestSreet" select u;
        foreach (var item in _Update)
        {
            item.Address = "WS";
        }
        MyDB.SubmitChanges();
        var Select = from s in MyDB.Employees select s;
        grd_1.ItemsSource = Select;

使用上面的代码我可以更新记录,我也能看到原始表数据和DataGrid中的记录修改。原始表已成功更新。

        MyDatatBaseDataContext MyDB = new MyDatatBaseDataContext();
        var _Udate_2 = MyDB.Employees.Where(u => (u.Address == "WestSreet"));
        foreach (var item in _Udate_2)
        {
            item.Address = "WS";
        }
        var Select = from s in MyDB.Employees select s;

        grd_1.ItemsSource = Select;

使用上面的代码,我可以更新记录。我可以看到DataGrid中的修改但是我看不到原始表数据中的记录修改。原始表没有更新。

2 个答案:

答案 0 :(得分:3)

你错过了

  MyDB.SubmitChanges();

在第二位代码上。

应该是这样的:

    MyDatatBaseDataContext MyDB = new MyDatatBaseDataContext();
     var _Udate_2 = MyDB.Employees.Where(u => (u.Address == "WestSreet"));
    foreach (var item in _Udate_2)
    {
        item.Address = "WS";
    }
    MyDB.SubmitChanges();
    var Select = from s in MyDB.Employees select s;

    grd_1.ItemsSource = Select;

答案 1 :(得分:2)

因为在第二段代码中你没有打电话给MyDB.SubmitChanges();

MyDatatBaseDataContext MyDB = new MyDatatBaseDataContext();
        var _Udate_2 = MyDB.Employees.Where(u => (u.Address == "WestSreet"));
        foreach (var item in _Udate_2)
        {
            item.Address = "WS";
        }
MyDB.SubmitChanges(); // Need to call that