/// <summary> /// 增加一条数据 /// </summary> public int Add(WebApi_Model.T_Product model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into T_Product("); strSql.Append("ProductName,Descriptions,TagID,RefProductID,RefPhotoID,Category1,Category2,LowPrice,OnSale,IsHot,IsNew,CoverPhoto,CreateDate,SalesVolume)"); strSql.Append(" values ("); strSql.Append("@ProductName,@Descriptions,@TagID,@RefProductID,@RefPhotoID,@Category1,@Category2,@LowPrice,@OnSale,@IsHot,@IsNew,@CoverPhoto,@CreateDate,@SalesVolume)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@ProductName", SqlDbType.NVarChar, 50), new SqlParameter("@Descriptions", SqlDbType.NVarChar, 400), new SqlParameter("@TagID", SqlDbType.NVarChar, 50), new SqlParameter("@RefProductID", SqlDbType.NVarChar, 50), new SqlParameter("@RefPhotoID", SqlDbType.NVarChar, 50), new SqlParameter("@Category1", SqlDbType.Int, 4), new SqlParameter("@Category2", SqlDbType.Int, 4), new SqlParameter("@LowPrice", SqlDbType.Decimal, 9), new SqlParameter("@OnSale", SqlDbType.Int, 4), new SqlParameter("@IsHot", SqlDbType.Int, 4), new SqlParameter("@IsNew", SqlDbType.Int, 4), new SqlParameter("@CoverPhoto", SqlDbType.NVarChar, 50), new SqlParameter("@CreateDate", SqlDbType.DateTime), new SqlParameter("@SalesVolume", SqlDbType.Int, 4) }; parameters[0].Value = model.ProductName; parameters[1].Value = model.Descriptions; parameters[2].Value = model.TagID; parameters[3].Value = model.RefProductID; parameters[4].Value = model.RefPhotoID; parameters[5].Value = model.Category1; parameters[6].Value = model.Category2; parameters[7].Value = model.LowPrice; parameters[8].Value = model.OnSale; parameters[9].Value = model.IsHot; parameters[10].Value = model.IsNew; parameters[11].Value = model.CoverPhoto; parameters[12].Value = model.CreateDate; parameters[13].Value = model.SalesVolume; object obj = DBHelper.GetSingle(strSql.ToString(), parameters); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 得到一个对象实体 /// </summary> public WebApi_Model.T_Product GetModel(int ProductID) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 * from T_Product "); strSql.Append(" where ProductID=@ProductID"); SqlParameter[] parameters = { new SqlParameter("@ProductID", SqlDbType.Int, 4) }; parameters[0].Value = ProductID; WebApi_Model.T_Product model = new WebApi_Model.T_Product(); DataSet ds = DBHelper.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return(DataRowToModel(ds.Tables[0].Rows[0])); } else { return(null); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(WebApi_Model.T_Product model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update T_Product set "); strSql.Append("ProductName=@ProductName,"); strSql.Append("Descriptions=@Descriptions,"); strSql.Append("TagID=@TagID,"); strSql.Append("RefProductID=@RefProductID,"); strSql.Append("RefPhotoID=@RefPhotoID,"); strSql.Append("Category1=@Category1,"); strSql.Append("Category2=@Category2,"); strSql.Append("LowPrice=@LowPrice,"); strSql.Append("OnSale=@OnSale,"); strSql.Append("IsHot=@IsHot,"); strSql.Append("IsNew=@IsNew,"); strSql.Append("CoverPhoto=@CoverPhoto,"); strSql.Append("CreateDate=@CreateDate,"); strSql.Append("SalesVolume=@SalesVolume"); strSql.Append(" where ProductID=@ProductID"); SqlParameter[] parameters = { new SqlParameter("@ProductName", SqlDbType.NVarChar, 50), new SqlParameter("@Descriptions", SqlDbType.NVarChar, 400), new SqlParameter("@TagID", SqlDbType.NVarChar, 50), new SqlParameter("@RefProductID", SqlDbType.NVarChar, 50), new SqlParameter("@RefPhotoID", SqlDbType.NVarChar, 50), new SqlParameter("@Category1", SqlDbType.Int, 4), new SqlParameter("@Category2", SqlDbType.Int, 4), new SqlParameter("@LowPrice", SqlDbType.Decimal, 9), new SqlParameter("@OnSale", SqlDbType.Int, 4), new SqlParameter("@IsHot", SqlDbType.Int, 4), new SqlParameter("@IsNew", SqlDbType.Int, 4), new SqlParameter("@CoverPhoto", SqlDbType.NVarChar, 50), new SqlParameter("@CreateDate", SqlDbType.DateTime), new SqlParameter("@SalesVolume", SqlDbType.Int, 4), new SqlParameter("@ProductID", SqlDbType.Int, 4) }; parameters[0].Value = model.ProductName; parameters[1].Value = model.Descriptions; parameters[2].Value = model.TagID; parameters[3].Value = model.RefProductID; parameters[4].Value = model.RefPhotoID; parameters[5].Value = model.Category1; parameters[6].Value = model.Category2; parameters[7].Value = model.LowPrice; parameters[8].Value = model.OnSale; parameters[9].Value = model.IsHot; parameters[10].Value = model.IsNew; parameters[11].Value = model.CoverPhoto; parameters[12].Value = model.CreateDate; parameters[13].Value = model.SalesVolume; parameters[14].Value = model.ProductID; int rows = DBHelper.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return(true); } else { return(false); } }
/// <summary> /// 得到一个对象实体 /// </summary> public WebApi_Model.T_Product DataRowToModel(DataRow row) { WebApi_Model.T_Product model = new WebApi_Model.T_Product(); if (row != null) { if (row["ProductID"] != null && row["ProductID"].ToString() != "") { model.ProductID = int.Parse(row["ProductID"].ToString()); } if (row["ProductName"] != null) { model.ProductName = row["ProductName"].ToString(); } if (row["Descriptions"] != null) { model.Descriptions = row["Descriptions"].ToString(); } if (row["TagID"] != null) { model.TagID = row["TagID"].ToString(); } if (row["RefProductID"] != null) { model.RefProductID = row["RefProductID"].ToString(); } if (row["RefPhotoID"] != null) { model.RefPhotoID = row["RefPhotoID"].ToString(); } if (row["Category1"] != null && row["Category1"].ToString() != "") { model.Category1 = int.Parse(row["Category1"].ToString()); } if (row["Category2"] != null && row["Category2"].ToString() != "") { model.Category2 = int.Parse(row["Category2"].ToString()); } if (row["LowPrice"] != null && row["LowPrice"].ToString() != "") { model.LowPrice = decimal.Parse(row["LowPrice"].ToString()); } if (row["OnSale"] != null && row["OnSale"].ToString() != "") { model.OnSale = int.Parse(row["OnSale"].ToString()); } if (row["IsHot"] != null && row["IsHot"].ToString() != "") { model.IsHot = int.Parse(row["IsHot"].ToString()); } if (row["IsNew"] != null && row["IsNew"].ToString() != "") { model.IsNew = int.Parse(row["IsNew"].ToString()); } if (row["CoverPhoto"] != null) { model.CoverPhoto = row["CoverPhoto"].ToString(); } if (row["CreateDate"] != null && row["CreateDate"].ToString() != "") { model.CreateDate = DateTime.Parse(row["CreateDate"].ToString()); } if (row["SalesVolume"] != null && row["SalesVolume"].ToString() != "") { model.SalesVolume = int.Parse(row["SalesVolume"].ToString()); } if (row["PropertyGroup"] != null && row["PropertyGroup"].ToString() != "") { model.PropertyGroup = row["PropertyGroup"].ToString(); } } return(model); }