/// <summary> /// 反射查询方法 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static List<T> QueryDataMethod<T>(string ID)where T:new() { //获取Type对象,反射操作基本都是用Type进行的 Type type = typeof(T); string strsql = "select * from @Table where ID=@id"; SqlParameter[] par = {new SqlParameter("@Table",type.Name), new SqlParameter("@id",ID), }; //调用执行方法 SqlDataReader dr= DBHelper.ExecuteReader(strsql,par); //获取Type对象的所有公共属性 PropertyInfo[] info = type.GetProperties(); List<T> modellist = new List<T>(); //定义泛型对象 T obj = default(T); while (dr.Read()) { obj = new T(); foreach (PropertyInfo item in info) { item.SetValue(obj, dr[item.Name]); } modellist.Add(obj); } dr.Close(); return modellist; }
/// <summary> /// 反射添加方法 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="nodel"></param> /// <returns></returns> public static int AddDataMethod<T>(T nodel) where T : new() { //获取Type对象,反射操作基本都是用Type进行的 Type type = typeof(T); string strsql = string.Format("insert into {0}", type.Name); string column = "("; string val = "values("; string link = ""; int index = 0; //获取Type对象所有公共属性 PropertyInfo[] info = type.GetProperties(); SqlParameter[] p = new SqlParameter[info.Count()]; foreach (PropertyInfo item in info) { column += link + item.Name;//定义需要添加的字段 val += link + " @" + item.Name;//定义字段变量 //给字段变量赋值 p[index++] = new SqlParameter("@" + item.Name, item.GetValue(nodel) != null ? item.GetValue(nodel) : DBNull.Value); link = ","; } column += ")"; val += ")"; strsql += column + val; //调用执行方法 return DBHelper.ExecuteNonQuery(strsql, p); }
/// <summary> /// 反射修改方法 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="nodel"></param> /// <returns></returns> public static int UpDataMethod<T>(T nodel,string id) where T : new() { //获取Type对象,反射操作基本都是用Type进行的 Type type = typeof(T); string strsql = string.Format("update {0} set ", type.Name); //获取Type对象所有公共属性 PropertyInfo[] info = type.GetProperties(); string link = ""; foreach (PropertyInfo item in info) { object val = item.GetValue(nodel); if (val != null) { //更新修改字段的值 strsql += link + item.Name + "='" + val + "'"; link = ","; } } strsql += string.Format(" where ID='{0}'", id); //调用执行方法 return DBHelper.ExecuteNonQuery(strsql); }
/// <summary> /// 反射删除方法 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static int DelDataMethod<T>(string id) where T : new() { //获取Type对象,反射操作基本都是用Type进行的 Type type = typeof(T); string strsql = string.Format("delete from {0} where ID='{1}'", type.Name,id); //调用执行方法 return DBHelper.ExecuteNonQuery(strsql); }
using System.Windows.Forms; using System.BLL; using System.Models; namespace Personal_Finance { public partial class FormTESt :Form { public FormTESt() { InitializeComponent(); } private void button_Click(object sender, EventArgs e) { //调用查询方法,注意这个方法是后面才改的名字 List<User> UserList = SystemBll.QueryDataListMethod<User>(); //调用添加方法 User user = new User(); user.user_Name = "小陈"; user.user_Phone = "13225547985"; if (SystemBll.AddDataMethod<User>(user) > 0) MessageBox.Show("添加成功"); else MessageBox.Show("添加失败"); //调用修改方法 user.user_ID = "001"; user.user_Name = "小李"; //user是修改对象,user_ID是修改条件 if (SystemBll.UpdateDataMethod<User>(user, user.user_ID) > 0) MessageBox.Show("修改成功"); else MessageBox.Show("修改失败"); //调用删除方法 if (SystemBll.DeleteDataMethod<User>(user.user_ID) > 0) MessageBox.Show("删除成功"); else MessageBox.Show("删除失败"); } } }