Example #1
0
        /// <summary>
        /// 利用泛型缓存插入数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool InsertGenericCacheDB <T>(T t) where T : BaseModel
        {
            Type type = typeof(T);

            //string proesStr = string.Join(",", type.GetProperties().
            //    Where(item => item.Name.ToLower() != "id").Select(item => "[" + item.Name + "]"));

            //string proesValStr = string.Join(",", type.GetProperties().
            //    Where(item => item.Name.ToLower() != "id").Select(item => "@" + item.Name));

            //string sqlStr = "Insert into " + "[" + type.Name + "]" + "values" + "(" + proesValStr + ")";


            string sqlStr = GenericCacheInsertDB <T> .GetInsertSql();

            int rows = 0;

            using (SqlConnection conn = new SqlConnection(ConnStrNetVipClsTweDB))
            {
                SqlCommand comm  = new SqlCommand(sqlStr, conn);
                var        proes = GetTypeProGenericCache <T> .GetTypeProInfos().Where(item => item.Name.ToLower() != "id");

                foreach (var pro in proes)
                {
                    SqlParameter para = new SqlParameter();
                    if (pro.IsDefined(typeof(DBNameAttribute), true))
                    {
                        DBNameAttribute dBNameAttribute = (DBNameAttribute)pro.GetCustomAttribute(typeof(DBNameAttribute));
                        para = new SqlParameter("@" + dBNameAttribute.Name, pro.GetValue(t));
                    }
                    else
                    {
                        para = new SqlParameter("@" + pro.Name, pro.GetValue(t));
                    }

                    comm.Parameters.Add(para);
                }
                conn.Open();
                try
                {
                    rows = comm.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    conn.Close();
                    throw e;
                }
            }
            if (rows > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// 利用泛型缓存插入数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool InsertGenericCacheDBDelegate <T>(T t) where T : BaseModel
        {
            Type   type   = typeof(T);
            string sqlStr = GenericCacheInsertDB <T> .GetInsertSql();

            int rows = 0;


            Func <SqlCommand, int> func = comm =>
            {
                var proes = GetTypeProGenericCache <T> .GetTypeProInfos().Where(item => item.Name.ToLower() != "id");

                foreach (var pro in proes)
                {
                    SqlParameter para = new SqlParameter();
                    if (pro.IsDefined(typeof(DBNameAttribute), true))
                    {
                        DBNameAttribute dBNameAttribute = (DBNameAttribute)pro.GetCustomAttribute(typeof(DBNameAttribute));
                        para = new SqlParameter("@" + dBNameAttribute.Name, pro.GetValue(t));
                    }
                    else
                    {
                        para = new SqlParameter("@" + pro.Name, pro.GetValue(t));
                    }
                    comm.Parameters.Add(para);
                }
                return(comm.ExecuteNonQuery());
            };

            rows = this.ExexuteSql(sqlStr, func);

            if (rows > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }