/// <summary> /// 得到一个对象实体 /// </summary> public MyBookShop.Model.Books DataRowToModel(DataRow row) { MyBookShop.Model.Books model = new MyBookShop.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 MyBookShop.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; MyBookShop.Model.Books model = new MyBookShop.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; } }