ORMHelper框架辅助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Reflection;
using Newtonsoft.Json;

namespace Dal
{
    public class OrmHelper
    {
        public int ExecuteNonQuery(string sql) {
            using (SqlConnection scon = new SqlConnection("server=.;uid=sa;pwd=1234;database=MySchool0001"))
            {
                scon.Open();
                SqlCommand scom = new SqlCommand(sql, scon);
                int result = scom.ExecuteNonQuery();
                scon.Close();
                return result;
            }   
        }

        public DataTable Query(string sql)
        {
            using (SqlConnection scon = new SqlConnection("server=.;uid=sa;pwd=1234;database=MySchool0001"))
            {
                SqlDataAdapter sada = new SqlDataAdapter(sql, scon);
                DataTable dt = new DataTable();
                sada.Fill(dt);
                return dt;
            }
        }

        public int Add<T>(T t) where  T:new ()
        {
            Type type = t.GetType();
            PropertyInfo[] ps = type.GetProperties();
            string[] names = ps.Select(p => p.Name).ToArray();
            string[] values = ps.Select(p => "'"+p.GetValue(t)+"'").ToArray();

            string valuestr = string.Join(",", values); 

            string sql = string.Format("insert into {0} values ({1})",type.Name,valuestr);

            return ExecuteNonQuery(sql);
        }

        public List<T> Select<T>()
        {
            //Type type = typeof(T);
            //PropertyInfo[] ps = type.GetProperties();
            //string[] names = ps.Select(p => p.Name).ToArray();
            //string namestr = string.Join(",", names);

            //string sql = string.Format("select {0} from {1}", namestr, type.Name);

            Type type = typeof(T);
            string sql = string.Format("select * from {0}",  type.Name);

            var dt = Query(sql);
            return JsonConvert.DeserializeObject<List<T>>(JsonConvert.SerializeObject(dt));
        }

        public int Delete<T>(int Id)
        {
            Type type = typeof(T);
            string sql = string.Format("delete from {0} where Id={1}", type.Name,Id);
            return ExecuteNonQuery(sql);
        }

        public int Update<T>(T t)
        {


            Type type = t.GetType();
            PropertyInfo[] ps = type.GetProperties();

            string temp = "";
            foreach (var p in ps)
            {
                temp+=string.Format("{0}='{1}',", p.Name,p.GetValue(t) );
            }
            temp = temp.Substring(0, temp.Length - 1);

            string sql = string.Format("update {0} set {1} where Id = {2}", type.Name, temp, type.GetProperty("Id").GetValue(t));

            return ExecuteNonQuery(sql);
        }
    }
}

发布了43 篇原创文章 · 获赞 35 · 访问量 1564

猜你喜欢

转载自blog.csdn.net/qq_45244974/article/details/103940090