林克submitchanges()未更新数据库

b5buobof  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(194)

我可以在应用程序打开时更新datagrid,但数据库中没有更新。所以当我再次打开应用程序时,我会看到我使用mssql手动添加到数据库中的对象。id没有主键。

private void AddEmployee_Click(object sender, RoutedEventArgs e)
    {
        using (var dataClasses1DataContext = new DataClasses1DataContext(Properties.Settings.Default.Database2ConnectionString))
        {
            try
            {
                var newEmployee = new Employee
                {
                    Id = int.Parse(idTextBox.Text),
                    first_name = firstNameTextBox.Text,
                    last_name = lastNameTextBox.Text,
                    birth_day = DateTime.Parse(dateOfBirthTextBox.Text),
                    sex = char.Parse(SexTextBox.Text),
                    salary = int.Parse(SalaryTextBox.Text),
                    supervisor_id = int.Parse(SupervisorIdTextBox.Text),
                    branch_id = int.Parse(BranchIdTextBox.Text)
                };

                dataClasses1DataContext.Employees.InsertOnSubmit(newEmployee);
                dataClasses1DataContext.SubmitChanges();

                DataGridEmployees.ItemsSource = null;
                DataGridEmployees.ItemsSource = dataClasses1DataContext.Employees.ToList();

                MessageBox.Show("Employee Added Successfully");
            }
            catch (FormatException)
            {
                MessageBox.Show(
                    "Invalid Input Values");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题