首页 > 其他分享 >net 泛型反射入门使用

net 泛型反射入门使用

时间:2024-05-22 22:19:29浏览次数:18  
标签:入门 System public new 泛型 using net SqlParameter users

目录结构

 


定义泛型反射 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

相关文章

  • Asp .Net Core 系列:集成 CAP + RabbitMQ + MySQL(含幂等性)
    简介官网:https://cap.dotnetcore.xyz/CAP是什么?是一个EventBus,同时也是一个在微服务或者SOA系统中解决分布式事务问题的一个框架。它有助于创建可扩展,可靠并且易于更改的微服务系统。什么是EventBus?事件总线是一种机制,它允许不同的组件彼此通信而不彼此了解。组件可以......
  • Gin 框架是怎么使用 net http 包的(gin.go)
     Gin框架是基于Go语言的标准库net/http构建的,它使用net/http提供的基础功能来构建自己的高性能Web应用框架。具体来说,Gin使用net/http的以下方面: 1,HandlerFunc:Gin使用net/http的HandlerFunc类型,这是一个可以作为HTTP处理程序的函数类型。Gin自己的路......
  • DataGridView 控件入门
    常用属性和方法ContextMenuStrip属性:当用户点击鼠标右键时(设置和contextMenuStrip挂钩)MultiSelect属性是否可以多行选择SelectionMode属性:设置选中方式,比如是否选中一整行(设置为FullRowSelect)Dock属性:设置显示位置AllowUserToAddRows属性:取......
  • kubernetes——资源管理
    Kubernetes资源管理介绍kubernetes中,所有的内容都抽象为资源,用户需要通过操作资源来管理kubernetes。kubernetes的最小管理单元是pod而不是容器,所以只能将容器放在pod中,而kubernetes一般也不会直接管理pod,而是通过pod控制器来管理pod的。pod可以提供服务之后,就要考虑如何访......
  • freebsd、openbsd、netbsd的区别
    开源BSD有三大系列:freebsd、openbsd、netbsd。其实MacOSX也是BSD系列,只不过是商业。1.FreeBSDFreeBSD是从386BSD的基础上发展起来的,而386BSD是由伯克利的计算机科学家BillJolitz开发的针对Intel80386芯片的一种BSD版本。因为这个原因,FreeBSD在32位体系的x86机器上总是运行......
  • Kubernetes集群中配置Ingress支持HTTPS访问(一):cfssl
    目录一.系统环境二.前言三.对称加密和非对称加密简介四.什么是HTTPS五.Ingress简介六.配置ingress对外发布服务6.1安装NGINXingresscontroller控制器6.2创建pod6.3为pod创建svc服务6.4使用ingress发布服务6.5访问服务6.5.1使用Linux客户端来访问服务6.5.2使用Windows客户......
  • .NET下免费开源的PDF类库(PDFSharp)
    前言目前.NET体系下常见的PDF类库有Aspose、QuestPDF、Spire、iTextSharp等,有一说一都挺好用的,我个人特别喜欢QuestPDF它基于C#FluentAPI提供全面的布局引擎;但是这些库要么属于商业库价格不菲(能理解收费),但是年费太贵了。要么是有条件限制开源的,如Spire开源版本有各种限制。i......
  • DataGridView treeview控件入门
    隐藏treeview相关联的线连接ShowLines设置为false设置行高:itemHeight设置在窗体的位置:Dock设置是否随窗体大小改变而改变:Anchor设置被选中后,是否占满整行:FullRowSelect被点击后的事件:AfterSelectprivatevoidtreeView_AfterSelec......
  • 上位机开发福利!快速掌握.NET中的Modbus通信
     安装nuget包 Wesky.Net.OpenTools  1.0.8或以上版本。支持.net framework 4.6以上版本,以及所有.net core以及以上版本引用。 开发一个简单的Winform界面,用来测试使用。如需该winform的demo,可以在公众号【Dotnet Dancer】后台,回复 modbus 即可获取。 通信连接......
  • .NET8 Identity Register
    分享给需要帮助的人:记一次IdentityAPI中注册的源码解读:设置用户账户为未验证状态,以及除此之外更安全的做法:延迟用户创建。包含了对优缺点的说明,以及适用场景。在ASP.NET8Identity中注册API的源码如下:routeGroup.MapPost("/register",asyncTask<Results<Ok,ValidationP......