本文介绍了不解析从LinqToSQLDataClass到Database LINQ C#WPF的更新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用C#WPF中的Linq在我的程序中构建一个编辑/更新系统。我的问题是我的代码确实提交给了LinqToSQLDatacontex,但没有将其解析为实际的数据库。

I'm building an Edit/Update system in my program using Linq in C# WPF. My problem is that my code does submit to the LinqToSQLDatacontex but doesn't parse it through to the actual Database.


结果是数据行在运行时更新但实际上并未更新实际数据库。

The result of that is that the datarow is updated in Runtime but in fact isn't updated in the actual Database.


用于更新我的行。

private void dgUsers_MouseUp(object sender, MouseButtonEventArgs e)
    {
        try
        {
            item = dgUsers.SelectedItem;
            string name = (dgUsers.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;

            var query =
                from t in db.tbl_Users
                where t.Name == name
                select t;

            foreach (var q in query)
            {
                tbMoreName.Text = q.Name;
                tbMoreRights.Text = q.Rights;
                tbMoreTag.Text = q.Operatortag;
                checkMoreActive.IsChecked = q.Active;
                tbMoreCardCode.Text = q.CardCode;
            }

            var table =
                from q in db.tbl_UserProfiles
                where q.Userprofile == tbMoreRights.Text
                select q;
        }
        catch (Exception exc)
        {
            MessageBox.Show("NOPE");
        }
    }


 private void btnSaveUser_Click(object sender, RoutedEventArgs e)
        {
            switch (saveType)
            {
                

                case "Edit":
                    #region save Edit User

                    var edit =
                        (from t in db.tbl_Users
                         where t.Name == name
                         select t).First();

                    MessageBox.Show(edit.Id.ToString() + " " + edit.Name.ToString() + " " + edit.Operatortag.ToString() + " " + edit.Rights.ToString() + " " + edit.Active.ToString());

                    edit.Id = edit.Id;
                    edit.Name = tbName.Text;
                    edit.Operatortag = tbOperatortag.Text;
                    edit.Rights = cbRights.Text;
                    edit.Active = checkActive.IsChecked.Value;
                    edit.CardCode = tbCardcode.Text;

                    MessageBox.Show(edit.Id.ToString() + " " + edit.Name.ToString() + " " + edit.Operatortag.ToString() + " " + edit.Rights.ToString() + " " + edit.Active.ToString() + " " + edit.CardCode.ToString());

                    db.SubmitChanges();

                    #endregion
                    saveType = "";
                    break;
            }
            var refresh =
                        (from q in db.tbl_Users
                         select new { Name = q.Name, Rights = q.Rights, Operatortag = q.Operatortag, Active = q.Active, Cardcode = q.CardCode }).ToList();

            dgUsers.ItemsSource = null;
            dgUsers.ItemsSource = refresh;
            MessageBox.Show(refresh[0].ToString() + " " + refresh[1].ToString() + " " + refresh[2].ToString() + " " + refresh[3].ToString() + " " + refresh[4].ToString());
        }










我希望你们其中一个人可以帮助我。提前致谢!!!


I hope that one of you guys can help me. Thanks in advance!!!

推荐答案

感谢您在这里发帖。

您是否遇到任何异常?

根据您的描述和相关代码,我创建了一个简单的演示,它运行良好,请检查您是否刷新数据表。

Based on your description and related code, I create a simple demo, it works well, please check if you refresh your data table.

此外,请检查您是否连接了正确的桌子。

In addition, please check if you connect the right table.

祝你好运,

Cole Wu


这篇关于不解析从LinqToSQLDataClass到Database LINQ C#WPF的更新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 22:35