目录结构
定义泛型反射 ToModel.cs文件
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Appraisal_System.Utility { //泛型反射 public static class ToModel { //什么是扩展方法-> 扩展方法就是在参数前面加个this //定义泛型 public static TModel DataRowToModel<TModel>(this DataRow rows) { //获取传进来的类型 Type type = typeof(TModel); TModel md = (TModel)Activator.CreateInstance(type); //实例化 //获取type里面有哪些属性 foreach (var prop in type.GetProperties()) { prop.SetValue(md, rows[prop.Name]); } return md; } } }
使用 Users.cs下的 ListAll方法
using Appraisal_System.Utility; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Appraisal_System.Models { public class Users { public int Id { get; set; } public string UserName { get; set; } public string Sex { get; set; } public int BaseTypeId { get; set; } public bool IsDel { get; set; } public string PassWord { get; set; } public static List<Users> ListALL() { string sql = "select u.Id,u.UserName,u.Sex,u.BaseTypeId,u.IsDel,a.AppraisalBase,a.BaseType from Users u left join AppraisalBases a on u.BaseTypeId=a.Id"; DataTable dt = SqlHelper.ExecuteTable(sql); List<Users> users = new List<Users>(); foreach (DataRow dr in dt.Rows) { users.Add(dr.DataRowToModel<Users>()); } return users; } public static int Insert(Users users) { string sql = "insert into Users(UserName,Sex,BaseTypeId,IsDel,PassWord) values(@UserName,@Sex,@BaseTypeId,@IsDel,@PassWord)"; SqlParameter[] pars = { new SqlParameter("@UserName", users.UserName), new SqlParameter("@Sex", users.Sex), new SqlParameter("@BaseTypeId", users.BaseTypeId), new SqlParameter("@IsDel",users.IsDel), new SqlParameter("@PassWord", users.PassWord) }; int count = SqlHelper.ExecuteNoQuery(sql, pars); return count; } public static int Update(Users users) { string sql = "update Users set UserName=@UserName,Sex=@Sex,BaseTypeId=@BaseTypeId,IsDel=@IsDel,PassWord=@PassWord where Id=@Id"; SqlParameter[] pars = { new SqlParameter("@UserName", users.UserName), new SqlParameter("@Sex", users.Sex), new SqlParameter("@BaseTypeId", users.BaseTypeId), new SqlParameter("@IsDel",users.IsDel), new SqlParameter("@PassWord", users.PassWord), new SqlParameter ("@Id", users.Id) }; int count = SqlHelper.ExecuteNoQuery(sql, pars); return count; } } }
FrmSetUser.cs 里面使用 Users.cs 里的 Insert方法
private void btnmSave_Click(object sender, EventArgs e) { string userName = textBox1.Text.Trim(); int baseTypeId = (int)cbxBase.SelectedValue; string sex = cbxSex.Text.Trim(); bool isDel = chklsStop.Checked; Users users = new Users { UserName = userName, BaseTypeId = baseTypeId, IsDel = isDel, Sex = sex, PassWord = "111" }; int count = Users.Insert(users); if(count > 0) { MessageBox.Show("数据添加成功", "提示", MessageBoxButtons.OK); } else { MessageBox.Show("数据添加失败", "提示", MessageBoxButtons.OK); } this.Close(); }
标签:入门,System,public,new,泛型,using,net,SqlParameter,users From: https://www.cnblogs.com/tlfe/p/18207238