linq to sql更新多个行 [英] linq to sql update mulitple rows

查看:89
本文介绍了linq to sql更新多个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用linq将sql表更新为sql:

I am trying to update a sql table using linq to sql :

使用Linq to SQL查询数据库我能够在WPF应用程序的UI中将数据加载到gridview中.因此,我有一个与每一行关联的复选框.该表具有EmpName和Ability作为两列.因此,当用户选中与行/行关联的复选框时,我必须在sql DB中更新表中的值.

Querying the database using Linq to SQL I am able to load the data into a gridview in the UI in a WPF application. So I have a check box associated with each row. The table has EmpName and Ability as two columns. So when user checks the check box associated with the row/rows I have to update the value in the table back in the sql DB.

这是我选中的复选框的代码:

Here is the code for my check box checked :

    private void EmployeeName_Checked(object sender, RoutedEventArgs e)
    {
      DataDataContext dc = new DataDataContext();

        foreach (var item in empListView.SelectedItems)
        {


        EmpList updateList  = (from p in dc. EmpList
                                          where p.able == No
                                          select p).FirstOrDefault();


            if(updateList.able==NO)
            updateList.able = YES;

        }

        dc.SubmitChanges();
        empListView.ItemsSource = dc.EmpLists.ToList();
    }

EmpList是我从设计器添加linqtosql类时创建的.因此,我尝试更新同一列表,从而将更改提交给数据库.

EmpList here is the one created when I added the linqtosql class from the designer. So I am trying to update the same list which inturn submits the changes to the DB.

问题:我能够基于gridview中的选定复选框选择行,并且能够遍历选定项,但是我无法更新所有选定行..只有很少的行正在更新...如果有人可以查看代码并在查询有任何问题的情况下指导我,我将感到非常高兴.

Question: I am able to select the rows based on the selected checkboxes in the gridview and I am able to loop through the selected items, but I am unable to update all the selected rows..only few are updating...I would be glad if some one can look at the code and guide me if theres any thing wrong with the query.

最终,我必须根据gridview的column Ability中的当前值更新所有选择的行.

Ultimately, I have to update all the selcted rows based on their current values in the colum Ability of the gridview.

推荐答案

DataDataContext dc = new DataDataContext();需要移到foreach循环之外.当前它不应该编译,因为它超出范围.我不明白您是如何将所选项目链接到要从数据集中提取的项目的,您只是提取第一个数据?您的商品是否有ID或要抢的东西?

DataDataContext dc = new DataDataContext(); needs to be moved outside of the foreach loop. Currently it should not compile because it is out of scope. I don't understand how you are linking the item selected to the item you are pulling from the data set, you are just pulling the first one? Does your item have an Id or something to grab?

这篇关于linq to sql更新多个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆