private void btnDelete_Click(object sender, RoutedEventArgs e) { if (this.comboBoxRecords.SelectedIndex == -1) { MessageBox.Show("请选择要删除的点名记录!", "操作提示", MessageBoxButton.OK, MessageBoxImage.Error); } else { if (MessageBox.Show(String.Format("您确定要删除“{0}”这条记录吗?", this.comboBoxRecords.Text), "操作提示", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK) { // 获取当前操作的课程id Guid id = (Guid)(this.comboBoxRecords.SelectedItem as ComboBoxItem).Tag; // 删除所有记录 dc.Record.DeleteAllOnSubmit(dc.Record.Where(t => t.IndexID == id)); // 删除记录索引 dc.RecordIndex.DeleteOnSubmit(dc.RecordIndex.First(t => t.ID == id)); // 记录删除操作 Codes.ChangeSet cs = new Codes.ChangeSet() { TableName = "RecordIndex", GuidID = id, Type = 1 }; dc.ChangeSet.InsertOnSubmit(cs); dc.SubmitChanges(); loadInfo(); } } }
// 删除 private void btnDelete_Click(object sender, RoutedEventArgs e) { ListBoxItem item = (ListBoxItem)this.listBoxCourses.SelectedItem; if (item == null) { MessageBox.Show("您还没有选择要操作的课程!", "操作提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } if (MessageBox.Show("您确定要删除 “" + item.Content.ToString() + "” 这门课吗,和此课程相关的其他信息也将删除?", "操作提示", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK) { var recordIndex = from i in dc.RecordIndex where i.CourseID == (Guid)item.Tag select i; foreach (Codes.RecordIndex ri in recordIndex) { // 删除和课程相关的记录 dc.Record.DeleteAllOnSubmit(dc.Record.Where(t => t.IndexID == ri.ID)); } // 删除和课程相关的索引 dc.RecordIndex.DeleteAllOnSubmit(recordIndex); dc.Course.DeleteOnSubmit(dc.Course.First(t => t.ID == (Guid)item.Tag)); // 保存修改记录 Codes.ChangeSet cs = new Codes.ChangeSet() { TableName = "Course", GuidID = (Guid)item.Tag, Type = 1 }; dc.ChangeSet.InsertOnSubmit(cs); dc.SubmitChanges(); loadCourses(); } }
// 修改课程 private void btnFix_Click(object sender, RoutedEventArgs e) { ListBoxItem item = (ListBoxItem)this.listBoxCourses.SelectedItem; if (item == null) { MessageBox.Show("您还没有选择要操作的课程!", "操作提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } Windows.NewCourseWindow newCourseWindow = new NewCourseWindow(); newCourseWindow.CourseName = item.Content.ToString(); newCourseWindow.Title = "修改课程"; if (newCourseWindow.ShowDialog() == true) { Codes.Course course = dc.Course.First(t => t.ID == (Guid)item.Tag); course.CourseName = newCourseWindow.CourseName; // 保存修改记录 Codes.ChangeSet cs = new Codes.ChangeSet() { TableName = "Course", GuidID = (Guid)item.Tag, Type = 0 }; dc.ChangeSet.InsertOnSubmit(cs); dc.SubmitChanges(); loadCourses(); } }
// 保存基本信息 private void btnSave_Click(object sender, RoutedEventArgs e) { if (validateInfo()) { // 保存数据的更改 if (dc.Settings.Where(a => a.Tag == "RollCall").Count() != 0) { Codes.Settings settings = dc.Settings.First(); settings.Admin = this.textBoxAdmin.Text; settings.ClassName = this.textBoxClass.Text; settings.Password = this.passwordBox.Password; settings.Phone = this.textBoxAdminPhone.Text; settings.ServerPath = this.textBoxServer.Text; // 保存修改记录 Codes.ChangeSet cs = new Codes.ChangeSet() { TableName = "Settings", Type = 0 }; dc.ChangeSet.InsertOnSubmit(cs); } else { Codes.Settings settings = new Codes.Settings() { Tag = "RollCall", Admin = this.textBoxAdmin.Text, ClassName = this.textBoxClass.Text, Password = this.passwordBox.Password, Phone = this.textBoxAdminPhone.Text, ServerPath = this.textBoxServer.Text }; dc.Settings.InsertOnSubmit(settings); } dc.SubmitChanges(); MessageBox.Show("信息已被成功保存!", "操作提示", MessageBoxButton.OK, MessageBoxImage.Information); } }
/// <summary> /// 将数据保存到数据库 /// </summary> private void saveToDataBase() { if (IsNew) { Windows.SelectCourseWindow course = new Windows.SelectCourseWindow(); course.ShowDialog(); Guid id = course.ID; Guid recordID = RecordIndexID; // 保存记录索引 Codes.RecordIndex index = new Codes.RecordIndex() { ID = recordID, CourseID = id, Date = DateTime.Now }; dc.RecordIndex.InsertOnSubmit(index); } // 保存所有学生记录 for (int i = 0; i < names.Length; i++) { // 一个学生的迟到情况 string contents = info[infos[i].SelectedIndex]; contents = contents == "已到" ? "" : contents; if (contents == "请假" && remarks[i] == "") { ReasonForLeaveWindow reason = new ReasonForLeaveWindow(); reason.Info = names[i].Text; reason.ShowDialog(); remarks[i] = reason.Reason; } // 判断是新键数据,还是修改数据 if (IsNew) { Codes.Record record = new Codes.Record() { ID = Guid.NewGuid(), IndexID = RecordIndexID, Contents = contents, Remark = remarks[i], StudentNo = Convert.ToInt32(names[i].Tag) }; dc.Record.InsertOnSubmit(record); } else { // 修改数据,本来有更简单的修改方法的,使用这种方法实属无奈,貌似实现Linq To SQLite 的Dblinq有问题 foreach (Codes.Record record in dc.Record.ToList()) { // 只有记录被修改才保存 if (record.ID == recordIDs[i] && oldContent[i] != contents) { record.Contents = contents; record.Remark = remarks[i]; // 保存修改记录 Codes.ChangeSet cs = new Codes.ChangeSet() { TableName = "Record", GuidID = record.ID, Type = 0 }; dc.ChangeSet.InsertOnSubmit(cs); } } } } // 提交数据更改 dc.SubmitChanges(); }