tnblog
首页
视频
资源
登录

访问数据库的泛型封装类

3342人阅读 2020/3/9 18:17 总访问:13762 评论:0 收藏:1 手机
分类: .net


        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="t">对象</param>
        /// <param name="flag">返回主键还是都影响行数  false  受影响行数  true 主键值</param>
        /// <returns></returns>
        public int Insert<T>(T t, bool flag = false) 
        {
            List<PropertyInfo> propertyInfos = new List<PropertyInfo>(typeof(T).GetProperties());

            //获取主键
            PropertyInfo pkey = propertyInfos.Where(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0).FirstOrDefault();

            propertyInfos.Remove(pkey);

            StringBuilder builder = new StringBuilder();
            builder.Append("insert into ").Append(typeof(T).Name).Append("(");

            StringBuilder valueBuilder = new StringBuilder();
            valueBuilder.Append(" values( ");

            for (int i = 0; i < propertyInfos.Count; i++)
            {
                PropertyInfo item = propertyInfos[i];
                if (item.GetValue(t) != null)
                {
                    builder.Append(item.Name);
                    valueBuilder.Append("'");
                    valueBuilder.Append(item.GetValue(t));
                    valueBuilder.Append("'");

                    if (i < propertyInfos.Count - 1)
                    {
                        builder.Append(",");
                        valueBuilder.Append(",");
                    }
                }
            }

            builder.Append(" ) ");
            valueBuilder.Append(" )");

            if (flag)
                builder.Append(" output inserted.").Append(pkey.Name);

            builder.Append(valueBuilder);

            using (SqlConnection conn = new SqlConnection(_connstr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(builder.ToString(), conn))
                {
                    if (flag)
                        return Convert.ToInt32(cmd.ExecuteScalar());//返回最新的主键
                    else
                        return cmd.ExecuteNonQuery();//返回受影响行数
                }
            }

        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public int Update<T>(T t)
        {
            List<PropertyInfo> propertyInfos = new List<PropertyInfo>(typeof(T).GetProperties());

            //获取主键
            PropertyInfo pkey = propertyInfos.Where(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Length > 0).FirstOrDefault();

            propertyInfos.Remove(pkey);

            StringBuilder builder = new StringBuilder();
            builder.Append("update ").Append(typeof(T).Name).Append(" set ");

            for (int i = 0; i < propertyInfos.Count; i++)
            {
                PropertyInfo item = propertyInfos[i];
                if (item.GetValue(t) != null)
                {
                    builder.Append(item.Name);
                    builder.Append("='");
                    builder.Append(item.GetValue(t));
                    builder.Append("'");

                    if (i < propertyInfos.Count - 1)
                        builder.Append(",");
                }
            }

            builder.Append(" where ");
            builder.Append(pkey.Name);
            builder.Append("='");
            builder.Append(pkey.GetValue(t));
            builder.Append("'");

            using (SqlConnection conn = new SqlConnection(_connstr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(builder.ToString(), conn))
                {
                    return cmd.ExecuteNonQuery();
                }
            }
        }


评价
没有个性,不需要签名
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术