/// <summary> /// 得到一个对象实体 /// </summary> public Model.Sys_Coupon_Details GetModel(int ID) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); Model.Sys_Coupon_Details model = new Model.Sys_Coupon_Details(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); foreach (PropertyInfo p in pros) { str1.Append(p.Name + ",");//拼接字段 } strSql.Append("select top 1 " + str1.ToString().Trim(',')); strSql.Append(" from Sys_Coupon_Details"); strSql.Append(" where ID=@ID"); SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4) }; parameters[0].Value = ID; DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0]; if (dt.Rows.Count > 0) { return(DataRowToModel(dt.Rows[0])); } else { return(null); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.Sys_Coupon_Details model) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <SqlParameter> paras = new List <SqlParameter>(); strSql.Append("update Sys_Coupon_Details set "); foreach (PropertyInfo pi in pros) { //如果不是主键则追加sql字符串 if (!pi.Name.Equals("ID")) { //判断属性值是否为空 if (pi.GetValue(model, null) != null) { str1.Append(pi.Name + "=@" + pi.Name + ","); //声明参数 paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值 } } } strSql.Append(str1.ToString().Trim(',')); strSql.Append(" where ID=@ID "); paras.Add(new SqlParameter("@ID", model.ID)); return(DbHelperSQL.ExecuteSql(strSql.ToString(), paras.ToArray()) > 0); }
/// <summary> /// 将对象转换实体 /// </summary> public Model.Sys_Coupon_Details DataRowToModel(DataRow row) { Model.Sys_Coupon_Details model = new Model.Sys_Coupon_Details(); if (row != null) { //利用反射获得属性的所有公共属性 Type modelType = model.GetType(); for (int i = 0; i < row.Table.Columns.Count; i++) { //查找实体是否存在列表相同的公共属性 PropertyInfo proInfo = modelType.GetProperty(row.Table.Columns[i].ColumnName); if (proInfo != null && row[i] != DBNull.Value) { proInfo.SetValue(model, row[i], null);//用索引值设置属性值 } } } return(model); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.Sys_Coupon_Details model) { StringBuilder strSql = new StringBuilder(); StringBuilder str1 = new StringBuilder(); //数据字段 StringBuilder str2 = new StringBuilder(); //数据参数 //利用反射获得属性的所有公共属性 PropertyInfo[] pros = model.GetType().GetProperties(); List <SqlParameter> paras = new List <SqlParameter>(); strSql.Append("insert into Sys_Coupon_Details("); foreach (PropertyInfo pi in pros) { //如果不是主键则追加sql字符串 if (!pi.Name.Equals("ID")) { //判断属性值是否为空 if (pi.GetValue(model, null) != null) { str1.Append(pi.Name + ","); //拼接字段 str2.Append("@" + pi.Name + ","); //声明参数 paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值 } } } strSql.Append(str1.ToString().Trim(',')); strSql.Append(") values ("); strSql.Append(str2.ToString().Trim(',')); strSql.Append(") "); strSql.Append(";select @@IDENTITY;"); object obj = DbHelperSQL.GetSingle(strSql.ToString(), paras.ToArray()); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
void Sys_Coupon(HttpContext context) { #region 处理请求参数 string Id = context.Request["Id"]; int pkid = 0; if (!string.IsNullOrEmpty(Id)) { pkid = Convert.ToInt32(Id); } string CType = StringHelper.NullToStr(context.Request["CType"]); string CName = StringHelper.NullToStr(context.Request["CName"]); string CCode = StringHelper.NullToStr(context.Request["CCode"]); string CSum = StringHelper.NullToStr(context.Request["CSum"]); string Deduction = StringHelper.NullToStr(context.Request["Deduction"]); string TDateS = StringHelper.NullToStr(context.Request["TDateS"]); string TDateE = StringHelper.NullToStr(context.Request["TDateE"]); int IssueNum = Utils.ToInt(StringHelper.NullToStr(context.Request["IssueNum"])); string Remark = StringHelper.NullToStr(context.Request["Remark"]); #endregion #region 随机数 Random ra = new Random(); string str = "qwertyuiopasdfghjklzxcvbnm_"; string reulst = ""; for (int i = 0; i < 6; i++) { reulst += str[ra.Next(str.Length)]; } #endregion string jsonRet = ""; string retMsg = ""; UserInfo user = new UserInfo(); if (pkid < 1) { #region 添加操作 Model.Sys_Coupon sys_Model = new Model.Sys_Coupon(); BLL.Sys_Coupon sys_BLL = new BLL.Sys_Coupon(); sys_Model.CType = CType; sys_Model.CName = CName; sys_Model.CCode = reulst; sys_Model.CSum = decimal.Parse(CSum); sys_Model.CDeduction = decimal.Parse(Deduction); sys_Model.StartingTime = Utils.ToDateTime(TDateS); sys_Model.EndTime = Utils.ToDateTime(TDateE); sys_Model.IssueNum = IssueNum; sys_Model.Remark = Remark; sys_Model.CreateUser = user.GetUserName(); sys_Model.CreateTime = DateTime.Now.ToLocalTime(); sys_Model.ModifyUser = user.GetUserName(); sys_Model.ModifyTime = DateTime.Now.ToLocalTime(); sys_Model.MoreCol1 = ""; sys_Model.MoreCol2 = ""; int ret = sys_BLL.Add(sys_Model); if (ret > 0) { int ret1 = 0; for (int i = 0; i < IssueNum; i++) { string num = ra.Next(1, 1000000).ToString(); Model.Sys_Coupon_Details sys_Model1 = new Model.Sys_Coupon_Details(); BLL.Sys_Coupon_Details sys_BLL1 = new BLL.Sys_Coupon_Details(); sys_Model1.CCode = reulst; sys_Model1.CSmallCode = reulst + "-" + num; sys_Model1.CNum = 1; sys_Model1.CTime = DateTime.Now.ToLocalTime(); sys_Model1.CState = "0"; sys_Model1.ReceiveTime = DateTime.Now.ToLocalTime(); ret1 = sys_BLL1.Add(sys_Model1); } if (ret1 > 0) { retMsg = "添加成功"; jsonRet = "{retMsg:\"" + retMsg + "\"}"; } } else { retMsg = "添加失败"; jsonRet = "{retMsg:\"" + retMsg + "\"}"; } context.Response.Write(retMsg); context.Response.End(); #endregion } else { #region 更新操作 Model.Sys_Coupon sys_Model = new BLL.Sys_Coupon().GetModel(pkid); BLL.Sys_Coupon sys_BLL = new BLL.Sys_Coupon(); sys_Model.ID = pkid; sys_Model.CType = CType; sys_Model.CName = CName; sys_Model.CCode = CCode; sys_Model.CSum = decimal.Parse(CSum); sys_Model.CDeduction = decimal.Parse(Deduction); sys_Model.StartingTime = Utils.ToDateTime(TDateS); sys_Model.EndTime = Utils.ToDateTime(TDateE); sys_Model.IssueNum = IssueNum; sys_Model.Remark = Remark; sys_Model.ModifyUser = user.GetUserName(); sys_Model.ModifyTime = DateTime.Now.ToLocalTime(); bool ret = sys_BLL.Update(sys_Model); if (ret == true) { new BLL.Sys_Public().Delete("Sys_Coupon_Details", "CCode='" + CCode + "'"); int ret1 = 0; for (int i = 0; i < IssueNum; i++) { string num = ra.Next(1, 1000000).ToString(); Model.Sys_Coupon_Details sys_Model1 = new Model.Sys_Coupon_Details(); BLL.Sys_Coupon_Details sys_BLL1 = new BLL.Sys_Coupon_Details(); sys_Model1.CCode = CCode; sys_Model1.CSmallCode = CCode + "-" + num; sys_Model1.CNum = 1; sys_Model1.CTime = DateTime.Now.ToLocalTime(); sys_Model1.CState = "0"; sys_Model1.ReceiveTime = DateTime.Now.ToLocalTime(); ret1 = sys_BLL1.Add(sys_Model1); } if (ret1 > 0) { retMsg = "更新成功"; jsonRet = "{retMsg:\"" + retMsg + "\"}"; } } else { retMsg = "更新失败"; jsonRet = "{retMsg:\"" + retMsg + "\"}"; } context.Response.Write(retMsg); context.Response.End(); #endregion } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.Sys_Coupon_Details model) { return(dal.Update(model)); }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.Sys_Coupon_Details model) { return(dal.Add(model)); }