亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#實現(xiàn)DataTable映射成Model的方法(附源碼)

 更新時間:2015年11月27日 11:17:29   作者:Jimmy.Yang  
這篇文章主要介紹了C#實現(xiàn)DataTable映射成Model的方法,以實例形式較為詳細(xì)的分析了DataTable映射成Model的具體步驟與相關(guān)技巧,并附帶了完整實例源碼供讀者下載,需要的朋友可以參考下

本文實例講述了C#實現(xiàn)DataTable映射成Model的方法。分享給大家供大家參考,具體如下:

這是數(shù)據(jù)庫開發(fā)中經(jīng)常遇到的問題,當(dāng)然,這可以用現(xiàn)成的ORM框架來解決,但有些時候,如果DataSet/DataTable是第三方接口返回的,ORM就不方便了,還得自己處理。

反射自然必不可少的,另外考慮到DataTable中的ColumnName通常與Model的PropertyName并不嚴(yán)格對應(yīng),可以用Attribute來記錄這種映射關(guān)系。

步驟1:先創(chuàng)建一個DataFieldAttribute類

using System;
namespace Jimmy.ORM
{
 [AttributeUsage(AttributeTargets.Property)]
 public sealed class DataFieldAttribute:Attribute
 {
 /// <summary>
 /// 表對應(yīng)的字段名
 /// </summary>
 public string ColumnName { set; get; }
 public DataFieldAttribute(string columnName)
 {
  ColumnName = columnName;
 }
 }
}

步驟2:在Model/Entity的Class成員上,應(yīng)用DataField特性,參見下面的代碼:

using System;
namespace Jimmy.ORM.Entity
{
 [Serializable]
 public class ProductEntity : DataEntityBase
 {
 [DataField("PRODUCT_NO")]
 public string ProductNo { set; get; }
 [DataField("PRODUCT_ID")]
 public int ProductId { set; get; }
 [DataField("PRODUCT_NAME")]
 public string ProductName { set; get; }
 public override string ToString()
 {
  return string.Format("ProductNo:{1}{0}ProductId:{2}{0}ProductName:{3}", Environment.NewLine, ProductNo,
     ProductId, ProductName);
 }
 }
}

步驟3:該反射出場了,為了方便起見,封裝了一個DataConvert類

using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace Jimmy.ORM
{
 /// <summary>
 /// 將DataRow/DataTable轉(zhuǎn)換成Entity/Entity列表
 /// </summary>
 public static class DataConvert<T> where T : DataEntityBase, new()
 {
 /// <summary>
 /// 將DataRow行轉(zhuǎn)換成Entity
 /// </summary>
 /// <param name="dr"></param>
 /// <returns></returns>
 public static T ToEntity(DataRow dr)
 {
  T entity = new T();
  Type info = typeof(T);
  var members = info.GetMembers();
  foreach (var mi in members)
  {
  if (mi.MemberType == MemberTypes.Property)
  {
   //讀取屬性上的DataField特性
   object[] attributes = mi.GetCustomAttributes(typeof(DataFieldAttribute), true);
   foreach (var attr in attributes)
   {
   var dataFieldAttr = attr as DataFieldAttribute;
   if (dataFieldAttr != null)
   {
    var propInfo = info.GetProperty(mi.Name);
    if (dr.Table.Columns.Contains(dataFieldAttr.ColumnName))
    {
    //根據(jù)ColumnName,將dr中的相對字段賦值給Entity屬性
    propInfo.SetValue(entity,
     Convert.ChangeType(dr[dataFieldAttr.ColumnName], propInfo.PropertyType),
     null);
    }
   }
   }
  }
  }
  return entity;
 }
 /// <summary>
 /// 將DataTable轉(zhuǎn)換成Entity列表
 /// </summary>
 /// <param name="dt"></param>
 /// <returns></returns>
 public static List<T> ToList(DataTable dt)
 {
  List<T> list = new List<T>(dt.Rows.Count);
  foreach (DataRow dr in dt.Rows)
  {
  list.Add(ToEntity(dr));
  }
  return list;
 }
 }
}

步驟4:測試

using System;
using System.Data;
using Jimmy.ORM.Entity;
namespace Jimmy.ORM.Test
{
 class Program
 {
 static void Main()
 {
  DataTable dt = new DataTable();
  dt.Columns.Add("PRODUCT_NO");
  dt.Columns.Add("PRODUCT_ID");
  dt.Columns.Add("PRODUCT_NAME");
  dt.Rows.Add("00001", 1, "手機(jī)");
  dt.Rows.Add("00002", 2, "服裝");
  var products = DataConvert<ProductEntity>.ToList(dt);
  foreach (var entity in products)
  {
  Console.WriteLine(entity);
  }
  Console.Read();
 }
 }
}

完整實例代碼代碼點擊此處本站下載

希望本文所述對大家C#程序設(shè)計有所幫助。

相關(guān)文章

最新評論