Ejemplo n.º 1
0
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ReceiptPayDetail GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1   = new StringBuilder();

            Model.ReceiptPayDetail model = new Model.ReceiptPayDetail();
            //利用反射获得属性的所有公共属性
            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 MS_ReceiptPayDetail");
            strSql.Append(" where rpd_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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.ReceiptPayDetail model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1   = new StringBuilder();

            //利用反射获得属性的所有公共属性
            PropertyInfo[]      pros  = model.GetType().GetProperties();
            List <SqlParameter> paras = new List <SqlParameter>();

            strSql.Append("update MS_ReceiptPayDetail set ");
            foreach (PropertyInfo pi in pros)
            {
                //如果不是主键则追加sql字符串
                if (!pi.Name.Equals("rpd_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 rpd_id=@id ");
            paras.Add(new SqlParameter("@id", model.rpd_id));
            return(DbHelperSQL.ExecuteSql(strSql.ToString(), paras.ToArray()) > 0);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.ReceiptPayDetail model, SqlConnection conn = null, SqlTransaction tran = null)
        {
            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 MS_ReceiptPayDetail(");
            foreach (PropertyInfo pi in pros)
            {
                //如果不是主键则追加sql字符串
                if (!pi.Name.Equals("rpd_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 = 0;

            if (tran == null)
            {
                obj = DbHelperSQL.GetSingle(strSql.ToString(), paras.ToArray());
            }
            else
            {
                obj = DbHelperSQL.GetSingle(conn, tran, strSql.ToString(), paras.ToArray());
            }
            if (obj == null)
            {
                return(0);
            }
            else
            {
                return(Convert.ToInt32(obj));
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 将对象转换实体
 /// </summary>
 public Model.ReceiptPayDetail DataRowToModel(DataRow row)
 {
     Model.ReceiptPayDetail model = new Model.ReceiptPayDetail();
     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);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 修改收款明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateReceiptDetail(Model.ReceiptPayDetail model, bool updateMoney)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1   = new StringBuilder();

            //利用反射获得属性的所有公共属性
            PropertyInfo[]      pros  = model.GetType().GetProperties();
            List <SqlParameter> paras = new List <SqlParameter>();

            strSql.Append("update MS_ReceiptPayDetail set ");
            foreach (PropertyInfo pi in pros)
            {
                //如果不是主键则追加sql字符串
                if (!pi.Name.Equals("rpd_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 rpd_id=@id ");
            paras.Add(new SqlParameter("@id", model.rpd_id));
            bool result = false;

            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();//打开数据连接
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 修改收款明细======================
                        int obj = DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), paras.ToArray());
                        #endregion

                        #region 修改收款通知==========================
                        if (obj > 0)
                        {
                            string sql = "select count(*) from MS_ReceiptPayDetail where rpd_rpid=@rpid";
                            List <SqlParameter> paras0 = new List <SqlParameter>();
                            paras0.Add(new SqlParameter("@rpid", model.rpd_rpid));
                            int count = Convert.ToInt32(DbHelperSQL.GetSingle(conn, trans, sql, paras0.ToArray()));
                            if (count > 0)
                            {
                                if (count == 1)
                                {
                                    //只有一条明细时,把收款对象,收款内容,收款金额,预收日期,收款方式一并更新到收款通知
                                    sql = "update MS_ReceiptPay set rp_cid=@cid,rp_content=@content,rp_money=@money,rp_foredate=@foredate,rp_method=@method where rp_id=@rpid and rp_isConfirm=0";
                                    List <SqlParameter> paras1 = new List <SqlParameter>();
                                    paras1.Add(new SqlParameter("@cid", model.rpd_cid));
                                    paras1.Add(new SqlParameter("@content", model.rpd_content));
                                    paras1.Add(new SqlParameter("@money", model.rpd_money));
                                    paras1.Add(new SqlParameter("@foredate", model.rpd_foredate));
                                    paras1.Add(new SqlParameter("@method", model.rpd_method));
                                    paras1.Add(new SqlParameter("@rpid", model.rpd_rpid));
                                    DbHelperSQL.ExecuteSql(conn, trans, sql, paras1.ToArray());
                                }
                                else
                                {
                                    //有多条明细时,只变更总金额到收款通知
                                    if (updateMoney)
                                    {
                                        sql = "update MS_ReceiptPay set rp_money=(select SUM(rpd_money) from MS_ReceiptPayDetail where rpd_rpid=@rpid) where rp_id=@rpid and rp_isConfirm=0";
                                        DbHelperSQL.ExecuteSql(conn, trans, sql, paras0.ToArray());
                                    }
                                }
                            }
                            trans.Commit();
                            result = true;
                        }
                        #endregion
                    }
                    catch (Exception err)
                    {
                        trans.Rollback(); //回滚事务
                        result = false;
                    }
                }
            }
            return(result);
        }