zoukankan      html  css  js  c++  java
  • ORM映射设计思想

    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 异步Test
    {
        public class SqlHelper
        {
            public void Test()
            {
                using (SqlConnection con = new SqlConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = "";
    
                        con.Open();
    
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                   var result= MapEntity<Person>(reader);
                                }
                            }
                        }
                    }
                }
            }
    
            private TResult MapEntity<TResult>(SqlDataReader reader) where TResult : new()
            {
                var properites = typeof(Person).GetProperties();
                var result = new TResult();
    
                foreach (var item in properites)
                {
                    var index = reader.GetOrdinal(item.Name);
                    var data = reader.GetValue(index);
    
                    //item.SetValue(person, data);
                    item.SetValue(result, Convert.ChangeType(data, item.PropertyType));
                }
    
                return result;
            }
        }
    
        public class Person
        {
            public string Id { get; set; }
    
            public string Name { get; set; }
    
            public string Gender { get; set; }
        }
    }
  • 相关阅读:
    2016.11.30
    java韩顺平老师视频有需要可以留言
    UESTC 1425 Another LCIS
    hdu 3308 LCIS
    HDU 3308 LCIS 线段树区间更新
    poj crane
    poj1436 Horizontally Visible Segments
    编程习惯记录
    poj 3225 Help with Intervals
    UVA 1513 Movie collection
  • 原文地址:https://www.cnblogs.com/liubiao/p/5230206.html
Copyright © 2011-2022 走看看