泛型反射ROM

这是一个通用的DBHelper的类文件

using System.Collections.Generic;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel.DataAnnotations;

namespace Reflectiones
{
    public class DBHelper
    {
        /// <summary>
        /// 显示数据
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public List<T> GetData<T>(T t) where T:class,new()
        {
            List<T> lst = new List<T>();

            var tf = typeof(T);
            string sql = "SELECT * FROM" + tf.Name;//Model层的类名
            DataTable dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(""))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                sda.Fill(dt);
            }

            PropertyInfo[] infos = tf.GetProperties();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                 t = new T();
                foreach (var item in infos)
                {
                    item.SetValue(t, dt.Rows[i][item.Name]);
                }
                lst.Add(t);
            }
            return lst;
        }
        public int ComSqlCreate<T>(T t)where T:class,new()
        {
            var tf = typeof(T);
            string sql = "INSERT INTO " + tf.Name + " VALUES(";
            PropertyInfo[] infos = tf.GetProperties();
            foreach (var item in infos)
            {
                if (item.GetCustomAttribute(typeof(KeyAttribute),true) == null)
                {
                    sql += "'" + item.GetValue(t, null) + "',";
                }
                
            }
            sql += sql.TrimEnd(',') + ")";
            using (SqlConnection conn = new SqlConnection(""))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                int n = cmd.ExecuteNonQuery();
                return n;
            }
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public int ComSqlUpdate<T>(T t) where T:class,new()
        {
            var tf = typeof(T);
            string sql = "UPDATE " + tf.Name + " SET ";
            string sqlWhere = " WHERE ";
            PropertyInfo[] infos = tf.GetProperties();
            foreach (var item in infos)
            {
                if (item.Name == "ID")
                {
                    sqlWhere += " WHERE "+ item.Name + " = '" + item.GetValue(t, null) + "'";
                }
                else
                {
                    sql += item.Name + " = '" + item.GetValue(t, null) + "',";
                }
            }
            sql = sql.TrimEnd(',') + sqlWhere;
            using (SqlConnection conn = new SqlConnection(""))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                int n = cmd.ExecuteNonQuery();
                return n;
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public int ComSqlDelete<T>(T t)where T:class,new()
        {
            var tf = typeof(T);
            PropertyInfo[] infos = tf.GetProperties();
            string sql = "DELETE FROM " + tf.Name;
            foreach (var item in infos)
            {
                if (item.Name == "ID")
                {
                    sql += " WHERE " + item.Name + " = '" + item.GetValue(t, null) + "'";
                }
            }
            using (SqlConnection conn = new SqlConnection(""))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                int n = cmd.ExecuteNonQuery();
                return n;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值