protected void btnSave_Click(object sender, EventArgs e) { string strErr = ""; if (this.txtusername.Text.Trim().Length == 0) { strErr += "username不能为空!\\n"; } if (!PageValidate.IsNumber(txtage.Text)) { strErr += "age格式错误!\\n"; } if (this.txtaddr.Text.Trim().Length == 0) { strErr += "addr不能为空!\\n"; } if (strErr != "") { MessageBox.Show(this, strErr); return; } string username = this.txtusername.Text; int age = int.Parse(this.txtage.Text); string addr = this.txtaddr.Text; ncu.mao.Model.t_user model = new ncu.mao.Model.t_user(); model.username = username; model.age = age; model.addr = addr; ncu.mao.BLL.t_user bll = new ncu.mao.BLL.t_user(); bll.Add(model); Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功!", "add.aspx"); }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(ncu.mao.Model.t_user model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update t_user set "); strSql.Append("username=@username,"); strSql.Append("age=@age,"); strSql.Append("addr=@addr"); strSql.Append(" where userid=@userid"); SqlParameter[] parameters = { new SqlParameter("@username", SqlDbType.VarChar, 50), new SqlParameter("@age", SqlDbType.Int, 4), new SqlParameter("@addr", SqlDbType.NVarChar, 50), new SqlParameter("@userid", SqlDbType.Int, 4) }; parameters[0].Value = model.username; parameters[1].Value = model.age; parameters[2].Value = model.addr; parameters[3].Value = model.userid; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 增加一条数据 /// </summary> public int Add(ncu.mao.Model.t_user model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into t_user("); strSql.Append("username,age,addr)"); strSql.Append(" values ("); strSql.Append("@username,@age,@addr)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@username", SqlDbType.VarChar, 50), new SqlParameter("@age", SqlDbType.Int, 4), new SqlParameter("@addr", SqlDbType.NVarChar, 50) }; parameters[0].Value = model.username; parameters[1].Value = model.age; parameters[2].Value = model.addr; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
private void ShowInfo(int userid) { ncu.mao.BLL.t_user bll = new ncu.mao.BLL.t_user(); ncu.mao.Model.t_user model = bll.GetModel(userid); this.lbluserid.Text = model.userid.ToString(); this.txtusername.Text = model.username; this.txtage.Text = model.age.ToString(); this.txtaddr.Text = model.addr; }
/// <summary> /// 得到一个对象实体 /// </summary> public ncu.mao.Model.t_user DataRowToModel(DataRow row) { ncu.mao.Model.t_user model = new ncu.mao.Model.t_user(); if (row != null) { if (row["userid"] != null && row["userid"].ToString() != "") { model.userid = int.Parse(row["userid"].ToString()); } if (row["username"] != null) { model.username = row["username"].ToString(); } if (row["age"] != null && row["age"].ToString() != "") { model.age = int.Parse(row["age"].ToString()); } if (row["addr"] != null) { model.addr = row["addr"].ToString(); } } return(model); }
/// <summary> /// 得到一个对象实体 /// </summary> public ncu.mao.Model.t_user GetModel(int userid) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 userid,username,age,addr from t_user "); strSql.Append(" where userid=@userid"); SqlParameter[] parameters = { new SqlParameter("@userid", SqlDbType.Int, 4) }; parameters[0].Value = userid; ncu.mao.Model.t_user model = new ncu.mao.Model.t_user(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }