泛型反射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;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Writing_the_future/article/details/85118607