Beispiel #1
0
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Dapper.Model.user_group_price GetModel(int group_id, int channel_id, int article_id)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1   = new StringBuilder();

            Dapper.Model.user_group_price model = new Dapper.Model.user_group_price();
            //利用反射获得属性的所有公共属性
            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 " + databaseprefix + "user_group_price");
            strSql.Append(" where group_id=@group_id and channel_id=@channel_id and article_id=@article_id");
            SqlParameter[] parameters =
            {
                new SqlParameter("@group_id",   SqlDbType.Int, 4),
                new SqlParameter("@channel_id", SqlDbType.Int, 4),
                new SqlParameter("@article_id", SqlDbType.Int, 4)
            };
            parameters[0].Value = group_id;
            parameters[1].Value = channel_id;
            parameters[2].Value = article_id;
            DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];

            if (dt.Rows.Count > 0)
            {
                return(DataRowToModel(dt.Rows[0]));
            }
            else
            {
                return(null);
            }
        }
Beispiel #2
0
 /// <summary>
 /// 将对象转换实体
 /// </summary>
 public Dapper.Model.user_group_price DataRowToModel(DataRow row)
 {
     Dapper.Model.user_group_price model = new Dapper.Model.user_group_price();
     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);
 }
Beispiel #3
0
        /// <summary>
        /// 增加一条数据,带事务
        /// </summary>
        public bool Add(SqlConnection conn, SqlTransaction trans, Dapper.Model.user_group_price model, int channel_id, int article_id)
        {
            StringBuilder strSql = new StringBuilder(); //SQL字符串
            StringBuilder str1   = new StringBuilder(); //数据库字段
            StringBuilder str2   = new StringBuilder(); //声明参数

            PropertyInfo[]      pros  = model.GetType().GetProperties();
            List <SqlParameter> paras = new List <SqlParameter>();

            strSql.Append("insert into " + databaseprefix + "user_group_price(");
            foreach (PropertyInfo pi in pros)
            {
                if (!pi.Name.Equals("id"))
                {
                    if (pi.GetValue(model, null) != null)
                    {
                        str1.Append(pi.Name + ",");
                        str2.Append("@" + pi.Name + ",");
                        switch (pi.Name)
                        {
                        case "channel_id":
                            paras.Add(new SqlParameter("@" + pi.Name, channel_id));
                            break;

                        case "article_id":
                            paras.Add(new SqlParameter("@" + pi.Name, article_id));
                            break;

                        default:
                            paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null)));
                            break;
                        }
                    }
                }
            }
            strSql.Append(str1.ToString().Trim(','));
            strSql.Append(") values (");
            strSql.Append(str2.ToString().Trim(','));
            strSql.Append(")");
            return(DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), paras.ToArray()) > 0);
        }