private void btn_Modify_Click(object sender, RoutedEventArgs e) { DataAccessLayer db = new DataAccessLayer(); Product modifiedProduct = new Product(); if (txt_ProductName.Text != OriginProduct.Name) { if (db.QueryProductByName(txt_ProductName.Text).Count() > 0) { SystemSounds.Beep.Play(); MessageBox.Show("已存在产品名称: " + txt_ProductName.Text + "!"); return; } } if (txt_ProductId.Text != OriginProduct.Id.ToString()) { if (db.QueryProductById(int.Parse(txt_ProductId.Text)).Count() > 0) { SystemSounds.Beep.Play(); MessageBox.Show("已存在产品编号:" + txt_ProductId.Text + "!"); return; } } modifiedProduct.Name = txt_ProductName.Text; try { modifiedProduct.Id = int.Parse(txt_ProductId.Text); } catch { SystemSounds.Beep.Play(); MessageBox.Show("产品编号必须为数字!"); return; } try { db.DeleteProductById(OriginProduct.Id); } catch { SystemSounds.Beep.Play(); MessageBox.Show("更改失败!"); return; } db.InsertProduct(modifiedProduct); pmPage.FillGridView_Product(); this.Close(); }
private void btn_Add_Click(object sender, RoutedEventArgs e) { DataAccessLayer db = new DataAccessLayer(); if (txt_ProductName.Text.Trim() == String.Empty) { SystemSounds.Beep.Play(); MessageBox.Show("请输入产品名称!"); return; } else if (db.QueryProductByName(txt_ProductName.Text).Count() > 0) { SystemSounds.Beep.Play(); MessageBox.Show("已存在产品名称:" + txt_ProductName + "!"); return; } else if (txt_ProductId.Text.Trim() == String.Empty) { SystemSounds.Beep.Play(); MessageBox.Show("请输入产品编号!"); return; } else if (db.QueryProductById(int.Parse(txt_ProductId.Text)).Count() > 0) { SystemSounds.Beep.Play(); MessageBox.Show("已存在产品编号:" + txt_ProductId.Text + "!"); return; } else { Product p = new Product(); p.Name = txt_ProductName.Text; try { p.Id = int.Parse(txt_ProductId.Text); } catch { SystemSounds.Beep.Play(); MessageBox.Show("产品编号必须为数字!"); return; } db.InsertProduct(p); pmPage.FillGridView_Product(); this.Close(); } }