/// <summary> /// 得到一个对象实体 /// </summary> public Craft DataRowToModel(DataRow row) { Craft model=new Craft(); if (row != null) { foreach (DataColumn col in row.Table.Columns) { if (row[col] != null && row[col].ToString() != "") { switch (col.ColumnName) { case "id": model.CraftID = int.Parse(row[col].ToString()); break; case "name": model.Name = row[col].ToString(); break; case "price": model.Price = decimal.Parse(row[col].ToString()); break; case "intro": model.Intro = row[col].ToString(); break; } } } } return model; }
/// <summary> /// 增加一条数据 /// </summary> public bool Add(Craft model) { StringBuilder strSql=new StringBuilder(); strSql.Append("insert into craft("); strSql.Append("name,price,intro)"); strSql.Append(" values ("); strSql.Append("?name,?price,?intro)"); MySqlParameter[] parameters = { new MySqlParameter("?name", MySqlDbType.VarChar,50), new MySqlParameter("?price", MySqlDbType.Decimal,12), new MySqlParameter("?intro", MySqlDbType.Text)}; parameters[0].Value = model.Name; parameters[1].Value = model.Price; parameters[2].Value = model.Intro; int rows=MysqlHelper.ExecuteNonQuery(strSql.ToString(),parameters); if (rows > 0) { return true; } else { return false; } }
public JsonResult AddFabric() { string strCraft = Request["craft"]; string strStyle = Request["style"]; string strCount = Request["count"]; string strID = Request["materialid"]; int id = 0; int count = 0; int carftID = 0; int styleID = 0; int.TryParse(strCraft,out carftID); int.TryParse(strStyle,out styleID); int.TryParse(strCount, out count); int.TryParse(strID, out id); JsonResult json = new JsonResult(); json.Data = new { flag = 0 }; if (count > 0 && carftID > 0 && styleID > 0&&id>0) { DetailItem item = new DetailItem(); item.Craft = carftID; item.Style=styleID; item.Type=CategoryType.Fabric; item.MaterialID=id; Material model = new Material(); MaterialOperation mop = new MaterialOperation(); model = mop.GetModel(id); Craft craftModel = new Craft(); CraftOperation craftOperation = new CraftOperation(); craftModel = craftOperation.GetModel(carftID); item.Count = count; if (model != null) { item.StylePrice = craftModel.Price; switch (styleID) { case 1: item.Price = model.Price ; break; case 2: item.Price = model.PriceHigh; break; case 3: item.Price = model.PriceFancy; break; } if (item.Price > 0) { MyCart.Add(item); json.Data = new { flag = 1,count=MyCart.Orders.Count,total=MyCart.TotalPrice }; } } } json.JsonRequestBehavior = JsonRequestBehavior.AllowGet; json.ContentEncoding = System.Text.Encoding.UTF8; return json; }
public JsonResult AjaxModel(int id) { JsonResult json = new JsonResult(); if (id > 0) { Craft model = new Craft(); CraftOperation cop = new CraftOperation(); model = cop.GetModel(id); if (model != null) { json.Data = model; json.JsonRequestBehavior = JsonRequestBehavior.AllowGet; json.ContentEncoding = System.Text.Encoding.UTF8; } } return json; }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Craft model) { StringBuilder strSql=new StringBuilder(); strSql.Append("update craft set "); strSql.Append("name=?name,"); strSql.Append("price=?price,"); strSql.Append("intro=?intro"); strSql.Append(" where id=?id"); MySqlParameter[] parameters = { new MySqlParameter("?name", MySqlDbType.VarChar,50), new MySqlParameter("?price", MySqlDbType.Decimal,12), new MySqlParameter("?intro", MySqlDbType.Text), new MySqlParameter("?id", MySqlDbType.Int32,10)}; parameters[0].Value = model.Name; parameters[1].Value = model.Price; parameters[2].Value = model.Intro; parameters[3].Value = model.CraftID; int rows=MysqlHelper.ExecuteNonQuery(strSql.ToString(),parameters); if (rows > 0) { return true; } else { return false; } }
/// <summary> /// 得到一个对象实体 /// </summary> public Craft GetModel(int id) { StringBuilder strSql=new StringBuilder(); strSql.Append("select id,name,price,intro from craft "); strSql.Append(" where id=?id"); MySqlParameter[] parameters = { new MySqlParameter("?id", MySqlDbType.Int32) }; parameters[0].Value = id; Craft model=new Craft(); DataSet ds=MysqlHelper.ExecuteDataSet(strSql.ToString(),parameters); if(ds.Tables[0].Rows.Count>0) { return DataRowToModel(ds.Tables[0].Rows[0]); } else { return null; } }
/// <summary> /// 增加一条数据 /// </summary> public bool Add(Craft model) { return dal.Add(model); }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Craft model) { return dal.Update(model); }