/// <summary> /// 保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnSave_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(tbxContent.Text.Trim())) { MessageBox.Show("日记还没写,不能保存的啊!"); return; } if (IsNew) { DiaryContent.Content = tbxContent.Text; DiaryContent.CreateDate = DateTime.Now; DiaryContent.Wather = tbxWather.Text.Trim(); DiaryContent.UserID = user.ID; using (var context = new MyBookDB()) { context.DiaryContent.Add(DiaryContent); context.SaveChanges(); } } else { DiaryContent.Content = tbxContent.Text; DiaryContent.Wather = tbxWather.Text.Trim(); using (var context = new MyBookDB()) { var old = context.DiaryContent.FirstOrDefault(t => t.ID == DiaryContent.ID); if (old == null) { context.DiaryContent.Add(DiaryContent); context.SaveChanges(); } else { context.DiaryContent.Remove(old); context.DiaryContent.Add(DiaryContent); context.SaveChanges(); } } } MessageBox.Show("保存成功咯!"); }
private void BtnOK_Click(object sender, EventArgs e) { using (var context = new MyBookDB()) { if (tbxNewPassword.Text == tbxOldPassword.Text) { MessageBox.Show("新旧密码相同,不需要修改啦!"); } var currentuserOld = context.User.FirstOrDefault(t => t.ID == user.ID); User newuser = new User(); newuser.PassWord = tbxNewPassword.Text; newuser.IsMainAccount = currentuserOld.IsMainAccount; context.User.Remove(currentuserOld); context.User.Add(newuser); context.SaveChanges(); MessageBox.Show("修改好咯!"); this.Close(); } }
private void BtnDelete_Click(object sender, EventArgs e) { if (this.dgvContent.SelectedRows.Count == 0) { return; } if (this.dgvContent.CurrentRow.Cells["clID"].Value != null) { var id = int.Parse(this.dgvContent.CurrentRow.Cells["clID"].Value.ToString()); using (var context = new MyBookDB()) { var currentcontent = context.DiaryContent.FirstOrDefault(t => t.ID == id); if (currentcontent == null) { return; } context.DiaryContent.Remove(currentcontent); context.SaveChanges(); //this.dgvContent.Rows.Remove(this.dgvContent.CurrentRow); MessageBox.Show("已经删除啦!"); } } Search(); }