//删除 private void btnDelete_Click(object sender, EventArgs e) { //删除操作,提示 DialogResult result = MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.OKCancel); if (result == DialogResult.OK) { //有选中行 if (this.dgvDataList.SelectedRows.Count > 0) { int id = Convert.ToInt32(lblId.Text); TblPersonBll bll = new TblPersonBll(); if (bll.DeletePersonById(id)) { //删除成功后,重新加载数据 LoadPersonData(); MessageBox.Show("删除成功!"); } else { MessageBox.Show("操作异常!"); } } else { MessageBox.Show("请选中行!"); } } }
private void frmSelect_Load(object sender, EventArgs e) { TblPersonBll bll = new TblPersonBll(); List <TblPerson> list = bll.SelectAll(); dataGridView1.DataSource = list; }
private void button1_Click(object sender, EventArgs e) { int id = Convert.ToInt32(txtId.Text.Trim()); TblPersonBll bll = new TblPersonBll(); int r = bll.Delete(id); MessageBox.Show("成功删除行" + r + "行"); }
//加载所有人员信息 void LoadPersonData() { TblPersonBll bll = new TblPersonBll(); List <TblPerson> list = bll.GetAllPerson(); //指定数据源 //1、我们可以自定义列的名称,不用模型绑定的字段名称 //2、对DataGridView中的,性别 内容进行格式化显示 //3、设置单行被选中,属性(MultiSelect)设置为 false //4、给 行 添加 进入事件处理方式 this.dgvDataList.DataSource = list; }
private void button1_Click(object sender, EventArgs e) { int id = Convert.ToInt32(txtId.Text.Trim()); TblPersonBll bll = new TblPersonBll(); //由于更新语句是更新表中的列,这时用户实际可能只更新部分列,那么对于那些其他的用户没有要更新的列,则还是更新成原来的值,所以这时需要先将数据库中的原来这行中德数据查询出来赋值给model,然后再更新。 TblPerson model = bll.GetModelById(id); model.AutoId = id; model.Uname = "蒋坤"; int r = bll.Update(model); MessageBox.Show("成功更新" + r + "行"); }
private void BtnAdd_Click(object sender, EventArgs e) { string name = txtName.Text.Trim(); int age = Convert.ToInt32(txtAge.Text.Trim()); int? height = string.IsNullOrEmpty(txtHeight.Text.Trim()) ? null : (int?)Convert.ToInt32(txtHeight.Text.Trim()); bool? gender = string.IsNullOrEmpty(cboGender.Text.Trim()) ? null : (bool?)(cboGender.Text == "男" ? true : false); TblPerson model = new TblPerson(); model.Uname = name; model.Age = age; model.Height = height; model.Gender = gender; TblPersonBll bll = new TblPersonBll(); int r = bll.Insert(model); MessageBox.Show("插入成功!自动编号" + r); }
//增加 private void btnAdd_Click(object sender, EventArgs e) { //1、采集数据 TblPerson person = new TblPerson(); person.Name = tbxName.Text.Trim(); person.Age = Convert.ToInt32(tbxAge.Text.Trim()); person.Height = Convert.ToInt32(tbxHeight.Text.Trim()); person.Gender = cbxGender.SelectedIndex == 0 ? null : (bool?)(cbxGender.SelectedIndex == 1 ? true : false); //2、实例化业务逻辑层,调用方法 TblPersonBll bll = new TblPersonBll(); int r = bll.AddPerson(person); if (r > 0) { MessageBox.Show("增加成功!"); LoadPersonData(); } }
private void btnUpdate_Click(object sender, EventArgs e) { //1、采集数据 TblPerson person = new TblPerson(); person.Id = Convert.ToInt32(lblId.Text); person.Name = tbxName.Text.Trim(); person.Age = Convert.ToInt32(tbxAge.Text.Trim()); person.Height = Convert.ToInt32(tbxHeight.Text.Trim()); person.Gender = cbxGender.SelectedIndex == 0 ? null : (bool?)(cbxGender.SelectedIndex == 1 ? true : false); //2、实例化业务逻辑层,调用方法 TblPersonBll bll = new TblPersonBll(); if (bll.UpdatePerson(person)) { //重新加载数据 LoadPersonData(); MessageBox.Show("修改成功!"); } }
//获取总人数 private void btnTest_Click(object sender, EventArgs e) { TblPersonBll bll = new TblPersonBll(); MessageBox.Show(string.Format("当前的人数为:{0}人", bll.GetPersonCount())); }