C# 利用反射将数据从SqlDataReader对象中,赋值给相应的Model

在与数据库的交互中,查询数据是经常要做的事情,每一组数据,我们都要去手写modle.属性 = dr["字段名"],这样去赋值,虽然各表的字段名,字段属性都不相同,但是回归本质,其实是同一个操作,于是,我便想到了用反射的方法,基于Model层,获取Model的全部属性,进行赋值,便可适用大部分的查询数据操作!
        public static TEntity MapEntity<TEntity>(SqlDataReader reader) where TEntity : class, new()
        {
            try
            {
                var properties = typeof(TEntity).GetProperties();
                var entity = new TEntity();
                foreach (var propertie in properties)
                {
                    if (propertie.CanWrite)//判断属性是否可写
                    {
                        try
                        {
                            var index = reader.GetOrdinal(propertie.Name.Replace("_", ""));//列序号
                            var data = reader.GetValue(index);//指定列值
                            if (data != DBNull.Value)
                            {
                                if (propertie.PropertyType.IsGenericType && propertie.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                                {
                                    propertie.SetValue(entity, Convert.ChangeType(data, propertie.PropertyType.GetGenericArguments()[0]), null);
                                }
                                else
                                {
                                    propertie.SetValue(entity, Convert.ChangeType(data, propertie.PropertyType), null);
                                }
                            }
                        }
                        catch (IndexOutOfRangeException ex)
                        {
                            continue;
                        }
                    }
                }
                return entity;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

调用
            while (dr.Read())
            {
                mod = Class1.MapEntity<stuModel>(dr);
                list.Add(mod);
            }

当然,利用反射性能上,就会和之前的方法相距甚远,于是,我们可以把反射出来的属性值集合参数化,避免多次反射,其实结果都是一样的,在C#编程中,面向对象的思想很重要,减少代码冗余,增加代码重用率,参数化是经常用到的思想;

参数化后:
        public static TEntity MapEntity<TEntity>(SqlDataReader reader, PropertyInfo[] properties) where TEntity : class, new()
        {
            try
            {
                // properties 为了避免多次反射,将其参数化,在C#编程中,应时刻有着面向对象的思想,而不是面向过程
                //var properties = typeof(TEntity).GetProperties();
                var entity = new TEntity();
                foreach (var propertie in properties)
                {
                    if (propertie.CanWrite)//判断属性是否可写
                    {
                        try
                        {
                            var index = reader.GetOrdinal(propertie.Name.Replace("_", ""));//列序号
                            var data = reader.GetValue(index);//指定列值
                            if (data != DBNull.Value)
                            {
                                if (propertie.PropertyType.IsGenericType && propertie.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                                {
                                    propertie.SetValue(entity, Convert.ChangeType(data, propertie.PropertyType.GetGenericArguments()[0]), null);
                                }
                                else
                                {
                                    propertie.SetValue(entity, Convert.ChangeType(data, propertie.PropertyType), null);
                                }
                            }
                        }
                        catch (IndexOutOfRangeException ex)
                        {
                            continue;
                        }
                    }
                }
                return entity;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

参数化后调用:
            var properties = typeof(stuModel).GetProperties();
            while (dr.Read())
            {
                mod = Class1.MapEntity<stuModel>(dr,properties);
               list.Add(mod);
            }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用List控件来显示数据数据信息。以下是一个使用C#和List控件的示例代码: ```csharp using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace ListExample { public partial class MainForm : Form { private string connectionString = "YourConnectionString"; // 替换为您的数据库连接字符串 public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { LoadData(); } private void LoadData() { // 创建连接对象 using (SqlConnection connection = new SqlConnection(connectionString)) { // 创建查询语句 string query = "SELECT * FROM YourTableName"; // 替换为您的表名 // 打开数据库连接 connection.Open(); // 创建Command对象 SqlCommand command = new SqlCommand(query, connection); // 执行查询并获取DataReader对象 SqlDataReader reader = command.ExecuteReader(); // 创建一个List来存储数据 List<string> dataList = new List<string>(); // 读取数据并添加到List while (reader.Read()) { string data = reader["ColumnName"].ToString(); // 替换为您的列名 dataList.Add(data); } // 关闭DataReader reader.Close(); // 将List绑定到List控件 listBox.DataSource = dataList; } } } } ``` 以上代码,您需要将`YourConnectionString`替换为实际的数据库连接字符串,将`YourTableName`替换为实际的表名,将`ColumnName`替换为实际的列名。然后在窗体加载事件调用`LoadData`方法即可将数据加载到List控件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值