SoFunction
Updated on 2025-03-07

C# implements the method of mapping DataTable into Model (with source code)

This article describes the method of C# to implement DataTable mapping to Model. Share it for your reference, as follows:

This is a common problem encountered in database development. Of course, this can be solved using the ready-made ORM framework. However, sometimes, if DataSet/DataTable is returned by a third-party interface, the ORM will be inconvenient and you have to deal with it yourself.

Reflection is naturally essential. In addition, considering that the ColumnName in DataTable does not strictly correspond to the Model PropertyName, you can use Attribute to record this mapping relationship.

Step 1: Create a DataFieldAttribute class first

using System;
namespace 
{
 [AttributeUsage()]
 public sealed class DataFieldAttribute:Attribute
 {
 /// <summary>
 /// The field name corresponding to the table /// </summary>
 public string ColumnName { set; get; }
 public DataFieldAttribute(string columnName)
 {
  ColumnName = columnName;
 }
 }
}

Step 2: Apply the DataField feature on the Class member of Model/Entity, see the following code:

using System;
namespace 
{
 [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 ("ProductNo:{1}{0}ProductId:{2}{0}ProductName:{3}", , ProductNo,
     ProductId, ProductName);
 }
 }
}

Step 3: It's time for reflection to appear. For convenience, a DataConvert class is encapsulated

using System;
using ;
using ;
using ;
namespace 
{
 /// <summary>
 /// Convert DataRow/DataTable to Entity/Entity list /// </summary>
 public static class DataConvert<T> where T : DataEntityBase, new()
 {
 /// <summary>
 /// Convert DataRow row to Entity /// </summary>
 /// <param name="dr"></param>
 /// <returns></returns>
 public static T ToEntity(DataRow dr)
 {
  T entity = new T();
  Type info = typeof(T);
  var members = ();
  foreach (var mi in members)
  {
  if ( == )
  {
   //Read the DataField attribute on the attribute   object[] attributes = (typeof(DataFieldAttribute), true);
   foreach (var attr in attributes)
   {
   var dataFieldAttr = attr as DataFieldAttribute;
   if (dataFieldAttr != null)
   {
    var propInfo = ();
    if (())
    {
    //According to ColumnName, assign the relative fields in dr to the Entity property    (entity,
     (dr[], ),
     null);
    }
   }
   }
  }
  }
  return entity;
 }
 /// <summary>
 /// Convert DataTable to Entity list /// </summary>
 /// <param name="dt"></param>
 /// <returns></returns>
 public static List<T> ToList(DataTable dt)
 {
  List<T> list = new List<T>();
  foreach (DataRow dr in )
  {
  (ToEntity(dr));
  }
  return list;
 }
 }
}

Step 4: Test

using System;
using ;
using ;
namespace 
{
 class Program
 {
 static void Main()
 {
  DataTable dt = new DataTable();
  ("PRODUCT_NO");
  ("PRODUCT_ID");
  ("PRODUCT_NAME");
  ("00001", 1, "cell phone");
  ("00002", 2, "clothing");
  var products = DataConvert<ProductEntity>.ToList(dt);
  foreach (var entity in products)
  {
  (entity);
  }
  ();
 }
 }
}

Click here for the full instance code codeDownload this site

I hope this article will be helpful to everyone's C# programming.