不会将更新的行从LinqToSQLDataClass解析为数据库LINQ C#WPF

时间:2017-03-09 10:31:34

标签: c# wpf linq linq-to-sql

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

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

这是我用于更新行的代码。

private void dgUsers_MouseUp(object sender, MouseButtonEventArgs e)
    {
        try
        {
            item = dgUsers.SelectedItem;
            this.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());
    }

我希望你们其中一个人可以帮助我。 在此先感谢!!!

0 个答案:

没有答案