/// <summary> /// 增加一条数据 /// </summary> public int Add(cn.itcast.bookshop.Model.Books model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into Books("); strSql.Append("Title,Author,PublisherId,PublishDate,ISBN,WordsCount,UnitPrice,ContentDescription,AurhorDescription,EditorComment,TOC,CategoryId,Clicks)"); strSql.Append(" values ("); strSql.Append("@Title,@Author,@PublisherId,@PublishDate,@ISBN,@WordsCount,@UnitPrice,@ContentDescription,@AurhorDescription,@EditorComment,@TOC,@CategoryId,@Clicks)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@Title", SqlDbType.NVarChar, 200), new SqlParameter("@Author", SqlDbType.NVarChar, 200), new SqlParameter("@PublisherId", SqlDbType.Int, 4), new SqlParameter("@PublishDate", SqlDbType.DateTime), new SqlParameter("@ISBN", SqlDbType.NVarChar, 50), new SqlParameter("@WordsCount", SqlDbType.Int, 4), new SqlParameter("@UnitPrice", SqlDbType.Money, 8), new SqlParameter("@ContentDescription", SqlDbType.NVarChar, -1), new SqlParameter("@AurhorDescription", SqlDbType.NVarChar, -1), new SqlParameter("@EditorComment", SqlDbType.NVarChar, -1), new SqlParameter("@TOC", SqlDbType.NVarChar, -1), new SqlParameter("@CategoryId", SqlDbType.Int, 4), new SqlParameter("@Clicks", SqlDbType.Int, 4) }; parameters[0].Value = model.Title; parameters[1].Value = model.Author; parameters[2].Value = model.PublisherId; parameters[3].Value = model.PublishDate; parameters[4].Value = model.ISBN; parameters[5].Value = model.WordsCount; parameters[6].Value = model.UnitPrice; parameters[7].Value = model.ContentDescription; parameters[8].Value = model.AurhorDescription; parameters[9].Value = model.EditorComment; parameters[10].Value = model.TOC; parameters[11].Value = model.CategoryId; parameters[12].Value = model.Clicks; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 得到一个对象实体 /// </summary> public cn.itcast.bookshop.Model.Books GetModel(int Id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 Id,Title,Author,PublisherId,PublishDate,ISBN,WordsCount,UnitPrice,ContentDescription,AurhorDescription,EditorComment,TOC,CategoryId,Clicks from Books "); strSql.Append(" where Id=@Id"); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Int, 4) }; parameters[0].Value = Id; cn.itcast.bookshop.Model.Books model = new cn.itcast.bookshop.Model.Books(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }
/// <summary> /// 得到一个对象实体 /// </summary> public cn.itcast.bookshop.Model.Books DataRowToModel(DataRow row) { cn.itcast.bookshop.Model.Books model = new cn.itcast.bookshop.Model.Books(); if (row != null) { if (row["Id"] != null && row["Id"].ToString() != "") { model.Id = int.Parse(row["Id"].ToString()); } if (row["Title"] != null) { model.Title = row["Title"].ToString(); } if (row["Author"] != null) { model.Author = row["Author"].ToString(); } if (row["PublisherId"] != null && row["PublisherId"].ToString() != "") { model.PublisherId = int.Parse(row["PublisherId"].ToString()); } if (row["PublishDate"] != null && row["PublishDate"].ToString() != "") { model.PublishDate = DateTime.Parse(row["PublishDate"].ToString()); } if (row["ISBN"] != null) { model.ISBN = row["ISBN"].ToString(); } if (row["WordsCount"] != null && row["WordsCount"].ToString() != "") { model.WordsCount = int.Parse(row["WordsCount"].ToString()); } if (row["UnitPrice"] != null && row["UnitPrice"].ToString() != "") { model.UnitPrice = decimal.Parse(row["UnitPrice"].ToString()); } if (row["ContentDescription"] != null) { model.ContentDescription = row["ContentDescription"].ToString(); } if (row["AurhorDescription"] != null) { model.AurhorDescription = row["AurhorDescription"].ToString(); } if (row["EditorComment"] != null) { model.EditorComment = row["EditorComment"].ToString(); } if (row["TOC"] != null) { model.TOC = row["TOC"].ToString(); } if (row["CategoryId"] != null && row["CategoryId"].ToString() != "") { model.CategoryId = int.Parse(row["CategoryId"].ToString()); } if (row["Clicks"] != null && row["Clicks"].ToString() != "") { model.Clicks = int.Parse(row["Clicks"].ToString()); } } return(model); }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(cn.itcast.bookshop.Model.Books model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update Books set "); strSql.Append("Title=@Title,"); strSql.Append("Author=@Author,"); strSql.Append("PublisherId=@PublisherId,"); strSql.Append("PublishDate=@PublishDate,"); strSql.Append("WordsCount=@WordsCount,"); strSql.Append("UnitPrice=@UnitPrice,"); strSql.Append("ContentDescription=@ContentDescription,"); strSql.Append("AurhorDescription=@AurhorDescription,"); strSql.Append("EditorComment=@EditorComment,"); strSql.Append("TOC=@TOC,"); strSql.Append("CategoryId=@CategoryId,"); strSql.Append("Clicks=@Clicks"); strSql.Append(" where Id=@Id"); SqlParameter[] parameters = { new SqlParameter("@Title", SqlDbType.NVarChar, 200), new SqlParameter("@Author", SqlDbType.NVarChar, 200), new SqlParameter("@PublisherId", SqlDbType.Int, 4), new SqlParameter("@PublishDate", SqlDbType.DateTime), new SqlParameter("@WordsCount", SqlDbType.Int, 4), new SqlParameter("@UnitPrice", SqlDbType.Money, 8), new SqlParameter("@ContentDescription", SqlDbType.NVarChar, -1), new SqlParameter("@AurhorDescription", SqlDbType.NVarChar, -1), new SqlParameter("@EditorComment", SqlDbType.NVarChar, -1), new SqlParameter("@TOC", SqlDbType.NVarChar, -1), new SqlParameter("@CategoryId", SqlDbType.Int, 4), new SqlParameter("@Clicks", SqlDbType.Int, 4), new SqlParameter("@Id", SqlDbType.Int, 4), new SqlParameter("@ISBN", SqlDbType.NVarChar, 50) }; parameters[0].Value = model.Title; parameters[1].Value = model.Author; parameters[2].Value = model.PublisherId; parameters[3].Value = model.PublishDate; parameters[4].Value = model.WordsCount; parameters[5].Value = model.UnitPrice; parameters[6].Value = model.ContentDescription; parameters[7].Value = model.AurhorDescription; parameters[8].Value = model.EditorComment; parameters[9].Value = model.TOC; parameters[10].Value = model.CategoryId; parameters[11].Value = model.Clicks; parameters[12].Value = model.Id; parameters[13].Value = model.ISBN; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }