程 序 人 生

[每个人都是工程师,书写着属于自己的人生代码!--林海]
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

LinQ实体表跟DataTable的数据互通

Posted on 2012-12-27 16:03  林海  阅读(205)  评论(0编辑  收藏  举报

var ddd = Nullable.GetUnderlyingType(typeof(int?));

DataTable dt = new DataTable();
Quot_Part_Model one = new Quot_Part_Model();
one.Part_Name = "AAA";
var properties = one.GetType().GetProperties();
foreach (var read in properties)
{
    var underlyingType = Nullable.GetUnderlyingType(read.PropertyType);
    if (underlyingType != null)
        dt.Columns.Add(read.Name, underlyingType);
    else
        dt.Columns.Add(read.Name, read.PropertyType);
    if (dt.Columns.Count <= 1)
        dt.Rows.Add();
    dt.Rows[0][read.Name] = read.GetValue(one, null) == null ? DBNull.Value : read.GetValue(one, null);
}

var row = dt.Rows[0];
row["User_ID"] = "97";
row["Part_Name"] = "BBBB";
row["Control_Visible"] = 111;
row["Process_List"] = "sdsd";
row["Create_Date"] = DateTime.Now;
foreach (var read in properties)
{
    if (row[read.Name] is DBNull)
        read.SetValue(one, null, null);
    else
        read.SetValue(one, row[read.Name], null);
}
}