最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

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

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

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:該反射出場(chǎng)了,為了方便起見(jiàn),封裝了一個(gè)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中的相對(duì)字段賦值給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:測(cè)試

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();
 }
 }
}

完整實(shí)例代碼代碼點(diǎn)擊此處本站下載。

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

相關(guān)文章

  • C#生成唯一值的方法匯總

    C#生成唯一值的方法匯總

    這篇文章主要介紹了C#生成唯一值的方法匯總,有需要的朋友可以參考一下
    2013-11-11
  • c#中如何獲取指定字符前的字符串

    c#中如何獲取指定字符前的字符串

    這篇文章主要介紹了c#中如何獲取指定字符前的字符串問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • C#畫(huà)圓角矩形的方法

    C#畫(huà)圓角矩形的方法

    這篇文章主要介紹了C#畫(huà)圓角矩形的方法,涉及C#繪圖的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-05-05
  • C#的WebBrowser的操作與注意事項(xiàng)介紹

    C#的WebBrowser的操作與注意事項(xiàng)介紹

    C#的WebBrowser的操作與注意事項(xiàng)介紹,需要的朋友可以參考一下
    2013-03-03
  • C#中加載dll并調(diào)用其函數(shù)的實(shí)現(xiàn)方法

    C#中加載dll并調(diào)用其函數(shù)的實(shí)現(xiàn)方法

    下面小編就為大家?guī)?lái)一篇C#中加載dll并調(diào)用其函數(shù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • C#多線程TPL常見(jiàn)操作誤區(qū)與異常處理

    C#多線程TPL常見(jiàn)操作誤區(qū)與異常處理

    本文詳細(xì)講解了C#多線程TPL常見(jiàn)操作誤區(qū)與異常處理,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • 比Math類庫(kù)abs()方法性能更高的取絕對(duì)值方法介紹

    比Math類庫(kù)abs()方法性能更高的取絕對(duì)值方法介紹

    這篇文章主要給大家介紹了一種比Math類庫(kù)abs()方法性能更高的取絕對(duì)值方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • C#并發(fā)編程入門(mén)教程之概述

    C#并發(fā)編程入門(mén)教程之概述

    這篇文章主要給大家介紹了關(guān)于C#并發(fā)編程之概述的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • ajaxFileUpload插件,C#返回Json數(shù)據(jù)報(bào)錯(cuò)問(wèn)題的解決方案

    ajaxFileUpload插件,C#返回Json數(shù)據(jù)報(bào)錯(cuò)問(wèn)題的解決方案

    這篇文章主要介紹了ajaxFileUpload插件,C#返回Json數(shù)據(jù)報(bào)錯(cuò)的解決方案,需要的朋友可以參考下
    2017-12-12
  • C# SMTP發(fā)送郵件的示例

    C# SMTP發(fā)送郵件的示例

    這篇文章主要介紹了C# SMTP發(fā)送郵件的示例,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-12-12

最新評(píng)論

朝阳区| 子长县| 扎赉特旗| 天等县| 商河县| 双牌县| 南雄市| 公安县| 普兰县| 历史| 平乡县| 杨浦区| 邵阳市| 仙游县| 柘城县| 喀喇沁旗| 汕头市| 大庆市| 本溪市| 林芝县| 宁德市| 横山县| 彭阳县| 大关县| 永修县| 陈巴尔虎旗| 瓦房店市| 安宁市| 石楼县| 宁远县| 河西区| 景泰县| 曲阜市| 漳浦县| 宾川县| 南雄市| 临猗县| 株洲市| 剑川县| 峨边| 犍为县|