Ejemplo n.º 1
0
        /// <summary>
        /// 删除数据库中的一条数据
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="id">数据id</param>
        /// <returns></returns>
        public bool DeleteGenericCacheDB <T>(int id) where T : BaseModel
        {
            Type type = typeof(T);
            //string sqlStr = "delete " + "[" + type.Name + "]" + " where id=@Id";

            string sqlStr = GenericCacheDel <Company> .GetDelSql();

            int rows = 0;

            using (SqlConnection conn = new SqlConnection(ConnStrNetVipClsTweDB))
            {
                SqlCommand   comm = new SqlCommand(sqlStr, conn);
                SqlParameter para = new SqlParameter("@Id", id);
                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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 删除数据库中的一条数据
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="id">数据id</param>
        /// <returns></returns>
        public bool DeleteGenericCacheDBDelegate <T>(int id) where T : BaseModel
        {
            Type type = typeof(T);
            //string sqlStr = "delete " + "[" + type.Name + "]" + " where id=@Id";

            string sqlStr = GenericCacheDel <T> .GetDelSql();

            int rows = 0;
            //using (SqlConnection conn = new SqlConnection(ConnStrNetVipClsTweDB))
            //{
            //    SqlCommand comm = new SqlCommand(sqlStr, conn);
            //    SqlParameter para = new SqlParameter("@Id", id);
            //    comm.Parameters.Add(para);
            //    conn.Open();
            //    try
            //    {
            //        rows = comm.ExecuteNonQuery();
            //    }
            //    catch (Exception e)
            //    {
            //        conn.Close();
            //        throw e;
            //    }

            //}

            Func <SqlCommand, int> func = comm =>
            {
                SqlParameter para = new SqlParameter("@Id", id);
                comm.Parameters.Add(para);
                return(comm.ExecuteNonQuery());
            };

            rows = this.ExexuteSql(sqlStr, func);


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