private void AppBar_Save_Click(object sender, EventArgs e) { try { //获取编辑的NoteTable对象 NoteTable note = (NoteTable)State["note"]; if (string.IsNullOrEmpty(TxtTitle.Text)) { MessageBox.Show("标题不能为空"); return; } if (TxtTitle.Text.Length > 10) { MessageBox.Show("标题不能超过10个字符"); } else { note.Title = TxtTitle.Text; } note.Content = TxtContent.Text; //保存数据库的改变 noteDB.SubmitChanges(); State["note"] = note; //保存完后直接跳到查询页面 NavigationService.Navigate(new Uri(string.Format("/MainPage.xaml"), UriKind.Relative)); } catch { MessageBox.Show("编辑出错!"); } }
private void AppBar_Delete_Click(object sender, EventArgs e) { try { //删除的NoteTable实例 NoteTable noteForDelete = State["note"] as NoteTable; // 移除数据库里面要删除的NoteTable记录 noteDB.Notes.DeleteOnSubmit(noteForDelete); //保存数据库的改变 noteDB.SubmitChanges(); //保存完后直接跳到查询页面 NavigationService.Navigate(new Uri(string.Format("/MainPage.xaml"), UriKind.Relative)); } catch { MessageBox.Show("删除失败!"); } }
private void AppBar_Save_Click(object sender, EventArgs e) { if (this.TxtTitle.Text == "") { MessageBox.Show("请填写标题"); return; } if (this.TxtTitle.Text.Length > 10) { MessageBox.Show("标题不能超过10个字符"); return; } try { //连接数据库并初始化DataContext实例 noteDB = new NoteDataContext(NoteDataContext.DBConnectionString); //创建一条表的数据 NoteTable newNote = new NoteTable { Title = TxtTitle.Text, Time = DateTime.Now.ToLongDateString(), Content = TxtContent.Text.ToString() }; //添加绑定集合的数据 noteCol.NoteTables.Add(newNote); //插入数据库 noteDB.Notes.InsertOnSubmit(newNote); //保存数据库的改变 noteDB.SubmitChanges(); } catch (Exception) { MessageBox.Show("保存数据出错!"); } //保存完后直接跳到查询页面 NavigationService.Navigate(new Uri(string.Format("/MainPage.xaml"), UriKind.Relative)); }