/// <summary> /// 增加一条数据 /// </summary> public int Add(OLBookstore.Model.ReaderComments model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into ReaderComments("); strSql.Append("BookId,ReaderName,Title,Comment,Date)"); strSql.Append(" values ("); strSql.Append("@BookId,@ReaderName,@Title,@Comment,@Date)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@BookId", SqlDbType.Int, 4), new SqlParameter("@ReaderName", SqlDbType.NChar, 10), new SqlParameter("@Title", SqlDbType.NVarChar, 100), new SqlParameter("@Comment", SqlDbType.NVarChar, 300), new SqlParameter("@Date", SqlDbType.DateTime) }; parameters[0].Value = model.BookId; parameters[1].Value = model.ReaderName; parameters[2].Value = model.Title; parameters[3].Value = model.Comment; parameters[4].Value = model.Date; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 得到一个对象实体 /// </summary> public OLBookstore.Model.ReaderComments DataRowToModel(DataRow row) { OLBookstore.Model.ReaderComments model = new OLBookstore.Model.ReaderComments(); if (row != null) { if (row["Id"] != null && row["Id"].ToString() != "") { model.Id = int.Parse(row["Id"].ToString()); } if (row["BookId"] != null && row["BookId"].ToString() != "") { model.BookId = int.Parse(row["BookId"].ToString()); } if (row["ReaderName"] != null) { model.ReaderName = row["ReaderName"].ToString(); } if (row["Title"] != null) { model.Title = row["Title"].ToString(); } if (row["Comment"] != null) { model.Comment = row["Comment"].ToString(); } if (row["Date"] != null && row["Date"].ToString() != "") { model.Date = DateTime.Parse(row["Date"].ToString()); } } return(model); }
private void ShowInfo(int Id) { OLBookstore.BLL.ReaderCommentsManager bll = new OLBookstore.BLL.ReaderCommentsManager(); OLBookstore.Model.ReaderComments model = bll.GetModel(Id); this.lblId.Text = model.Id.ToString(); this.txtBookId.Text = model.BookId.ToString(); this.lblReaderName.Text = model.ReaderName; this.txtTitle.Text = model.Title; this.txtComment.Text = model.Comment; this.txtDate.Text = model.Date.ToString(); }
protected void btnSave_Click(object sender, EventArgs e) { string strErr = ""; if (!PageValidate.IsNumber(txtBookId.Text)) { strErr += "BookId格式错误!\\n"; } if (this.txtReaderName.Text.Trim().Length == 0) { strErr += "ReaderName不能为空!\\n"; } if (this.txtTitle.Text.Trim().Length == 0) { strErr += "Title不能为空!\\n"; } if (this.txtComment.Text.Trim().Length == 0) { strErr += "Comment不能为空!\\n"; } if (!PageValidate.IsDateTime(txtDate.Text)) { strErr += "Date格式错误!\\n"; } if (strErr != "") { MessageBox.Show(this, strErr); return; } int BookId = int.Parse(this.txtBookId.Text); string ReaderName = this.txtReaderName.Text; string Title = this.txtTitle.Text; string Comment = this.txtComment.Text; DateTime Date = DateTime.Parse(this.txtDate.Text); OLBookstore.Model.ReaderComments model = new OLBookstore.Model.ReaderComments(); model.BookId = BookId; model.ReaderName = ReaderName; model.Title = Title; model.Comment = Comment; model.Date = Date; OLBookstore.BLL.ReaderCommentsManager bll = new OLBookstore.BLL.ReaderCommentsManager(); bll.Add(model); Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功!", "add.aspx"); }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(OLBookstore.Model.ReaderComments model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update ReaderComments set "); strSql.Append("BookId=@BookId,"); strSql.Append("Title=@Title,"); strSql.Append("Comment=@Comment,"); strSql.Append("Date=@Date"); strSql.Append(" where Id=@Id"); SqlParameter[] parameters = { new SqlParameter("@BookId", SqlDbType.Int, 4), new SqlParameter("@Title", SqlDbType.NVarChar, 100), new SqlParameter("@Comment", SqlDbType.NVarChar, 300), new SqlParameter("@Date", SqlDbType.DateTime), new SqlParameter("@Id", SqlDbType.Int, 4), new SqlParameter("@ReaderName", SqlDbType.NChar, 10) }; parameters[0].Value = model.BookId; parameters[1].Value = model.Title; parameters[2].Value = model.Comment; parameters[3].Value = model.Date; parameters[4].Value = model.Id; parameters[5].Value = model.ReaderName; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 得到一个对象实体 /// </summary> public OLBookstore.Model.ReaderComments GetModel(int Id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 Id,BookId,ReaderName,Title,Comment,Date from ReaderComments "); strSql.Append(" where Id=@Id"); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Int, 4) }; parameters[0].Value = Id; OLBookstore.Model.ReaderComments model = new OLBookstore.Model.ReaderComments(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }