首页 > 其他 > 详细

ORM映射设计思想

时间:2016-03-01 12:49:11      阅读:286      评论:0      收藏:0      [点我收藏+]
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; }
    }
}

 

ORM映射设计思想

原文:http://www.cnblogs.com/liubiao/p/5230206.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!