/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.tb_kehu model) { StringBuilder strSql = new StringBuilder(); StringBuilder strSql1 = new StringBuilder(); StringBuilder strSql2 = new StringBuilder(); if (model.h_no != null) { strSql1.Append("h_no,"); strSql2.Append("'" + model.h_no + "',"); } if (model.h_name != null) { strSql1.Append("h_name,"); strSql2.Append("'" + model.h_name + "',"); } if (model.h_tel != null) { strSql1.Append("h_tel,"); strSql2.Append("'" + model.h_tel + "',"); } if (model.h_address != null) { strSql1.Append("h_address,"); strSql2.Append("'" + model.h_address + "',"); } if (model.h_zipcode != null) { strSql1.Append("h_zipcode,"); strSql2.Append("'" + model.h_zipcode + "',"); } if (model.h_uname != null) { strSql1.Append("h_uname,"); strSql2.Append("'" + model.h_uname + "',"); } if (model.h_remark != null) { strSql1.Append("h_remark,"); strSql2.Append("'" + model.h_remark + "',"); } strSql.Append("insert into tb_kehu("); strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1)); strSql.Append(")"); strSql.Append(" values ("); strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1)); strSql.Append(")"); strSql.Append(";select @@IDENTITY"); object obj = DbSQL.GetSingle(strSql.ToString()); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 得到一个对象实体 /// </summary> public Model.tb_kehu GetModel(int h_id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 "); strSql.Append(" h_id,h_no,h_name,h_tel,h_address,h_zipcode,h_uname,h_remark "); strSql.Append(" from tb_kehu "); strSql.Append(" where h_id=" + h_id + ""); Model.tb_kehu model = new Model.tb_kehu(); DataSet ds = DbSQL.Query(strSql.ToString()); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["h_id"] != null && ds.Tables[0].Rows[0]["h_id"].ToString() != "") { model.h_id = int.Parse(ds.Tables[0].Rows[0]["h_id"].ToString()); } if (ds.Tables[0].Rows[0]["h_no"] != null && ds.Tables[0].Rows[0]["h_no"].ToString() != "") { model.h_no = ds.Tables[0].Rows[0]["h_no"].ToString(); } if (ds.Tables[0].Rows[0]["h_name"] != null && ds.Tables[0].Rows[0]["h_name"].ToString() != "") { model.h_name = ds.Tables[0].Rows[0]["h_name"].ToString(); } if (ds.Tables[0].Rows[0]["h_tel"] != null && ds.Tables[0].Rows[0]["h_tel"].ToString() != "") { model.h_tel = ds.Tables[0].Rows[0]["h_tel"].ToString(); } if (ds.Tables[0].Rows[0]["h_address"] != null && ds.Tables[0].Rows[0]["h_address"].ToString() != "") { model.h_address = ds.Tables[0].Rows[0]["h_address"].ToString(); } if (ds.Tables[0].Rows[0]["h_zipcode"] != null && ds.Tables[0].Rows[0]["h_zipcode"].ToString() != "") { model.h_zipcode = ds.Tables[0].Rows[0]["h_zipcode"].ToString(); } if (ds.Tables[0].Rows[0]["h_uname"] != null && ds.Tables[0].Rows[0]["h_uname"].ToString() != "") { model.h_uname = ds.Tables[0].Rows[0]["h_uname"].ToString(); } if (ds.Tables[0].Rows[0]["h_remark"] != null && ds.Tables[0].Rows[0]["h_remark"].ToString() != "") { model.h_remark = ds.Tables[0].Rows[0]["h_remark"].ToString(); } return(model); } else { return(null); } }
private void dgvCellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) { return; } optrowid = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); if (!String.IsNullOrEmpty(optrowid)) { model = dal.GetModel(Int32.Parse(optrowid)); if (model != null) { txtno.Text = model.h_no; txtname.Text = model.h_name; txttel.Text = model.h_tel; txtuname.Text = model.h_uname; txtaddress.Text = model.h_address; txtzipcode.Text = model.h_zipcode; } } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.tb_kehu model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tb_kehu set "); if (model.h_no != null) { strSql.Append("h_no='" + model.h_no + "',"); } else { strSql.Append("h_no= null ,"); } if (model.h_name != null) { strSql.Append("h_name='" + model.h_name + "',"); } else { strSql.Append("h_name= null ,"); } if (model.h_tel != null) { strSql.Append("h_tel='" + model.h_tel + "',"); } else { strSql.Append("h_tel= null ,"); } if (model.h_address != null) { strSql.Append("h_address='" + model.h_address + "',"); } else { strSql.Append("h_address= null ,"); } if (model.h_zipcode != null) { strSql.Append("h_zipcode='" + model.h_zipcode + "',"); } else { strSql.Append("h_zipcode= null ,"); } if (model.h_uname != null) { strSql.Append("h_uname='" + model.h_uname + "',"); } else { strSql.Append("h_uname= null ,"); } if (model.h_remark != null) { strSql.Append("h_remark='" + model.h_remark + "',"); } else { strSql.Append("h_remark= null ,"); } int n = strSql.ToString().LastIndexOf(","); strSql.Remove(n, 1); strSql.Append(" where h_id=" + model.h_id + ""); int rowsAffected = DbSQL.ExecuteSql(strSql.ToString()); if (rowsAffected > 0) { return(true); } else { return(false); } }
private void tbBtnClick(object sender, ToolBarButtonClickEventArgs e) { if (e.Button.ToolTipText == "新增") { rstValue(); setModifyMode(true); optrowid = null; } if (e.Button.ToolTipText == "修改") { if (!String.IsNullOrEmpty(optrowid)) { setModifyMode(true); } else { MessageBox.Show("请选择所要修改的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } if (e.Button.ToolTipText == "删除") { if (!String.IsNullOrEmpty(optrowid)) { DialogResult result = MessageBox.Show("确认删除?", "删除数据", MessageBoxButtons.OKCancel); if (result == DialogResult.OK) { dal.Delete(int.Parse(optrowid)); rstValue(); MessageBox.Show("恭喜你,删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); bindData(" "); setModifyMode(false); optrowid = null; } } else { MessageBox.Show("请选择所要删除的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } if (e.Button.ToolTipText == "提交") { if (validateIput()) { model = new Model.tb_kehu(); if (!String.IsNullOrEmpty(optrowid)) { model = dal.GetModel(int.Parse(optrowid)); } model.h_no = this.txtno.Text; model.h_name = this.txtname.Text; model.h_tel = this.txttel.Text; model.h_uname = this.txtuname.Text; model.h_address = this.txtaddress.Text; model.h_zipcode = this.txtzipcode.Text; if (String.IsNullOrEmpty(optrowid)) { int i = dal.Add(model); if (i > 0) { MessageBox.Show("恭喜你,新增成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); rstValue(); bindData(" "); setModifyMode(false); optrowid = null; } } else { if (dal.Update(model)) { MessageBox.Show("恭喜你,修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); rstValue(); bindData(" "); setModifyMode(false); optrowid = null; } } } } if (e.Button.ToolTipText == "取消") { bindData(" "); rstValue(); setModifyMode(false); optrowid = null; } if (e.Button.ToolTipText == "退出") { this.Close(); } }