DataTable List与转帮助类
2020-07-07 20:15 idea555 阅读(105) 评论(0) 收藏 举报public static class DataTableHelper
{
/// <summary>
/// DataTable转实体对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> TableToEntity<T>(System.Data.DataTable dt) where T : class, new()
{
// 定义集合
List<T> ts = new List<T>();
// 获得此模型的类型
Type type = typeof(T);
//定义一个临时变量
string tempName = string.Empty;
//遍历DataTable中所有的数据行
foreach (DataRow dr in dt.Rows)
{
T t = new T();
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
//遍历该对象的所有属性
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;//将属性名称赋值给临时变量
//检查DataTable是否包含此列(列名==对象的属性名)
if (dt.Columns.Contains(tempName))
{
// 判断此属性是否有Setter
if (!pi.CanWrite) continue;//该属性不可写,直接跳出
//取值
object value = dr[tempName];
//如果非空,则赋给对象的属性
if (dr[pi.Name] is Int64)
{
if (pi.GetMethod.ReturnParameter.ParameterType.Name == "Int32")
{
pi.SetValue(t, Convert.ToInt32(dr[pi.Name]), null);
}
else
{
pi.SetValue(t, Convert.ToInt64(dr[pi.Name]), null);
}
continue;
}
if (dr[pi.Name] is Int32)
{
pi.SetValue(t, Convert.ToInt32(dr[pi.Name]), null);
continue;
}
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
//对象添加到泛型集合中
ts.Add(t);
}
return ts;
}
#region List转DataTable
public static System.Data.DataTable ToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
System.Data.DataTable dt = new System.Data.DataTable();
for (int i = 0; i < properties.Count; i++)
{
PropertyDescriptor property = properties[i];
dt.Columns.Add(property.Name, property.PropertyType);
}
object[] values = new object[properties.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = properties[i].GetValue(item);
}
dt.Rows.Add(values);
}
return dt;
}
#endregion List转DataTable
/// <summary>
/// DataTable转实体对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
//public static List<T> TableToEntity<T>(System.Data.DataTable dt) where T : class, new()
//{
// // 定义集合
// List<T> ts = new List<T>();
// // 获得此模型的类型
// Type type = typeof(T);
// //定义一个临时变量
// string tempName = string.Empty;
// //遍历DataTable中所有的数据行
// foreach (DataRow dr in dt.Rows)
// {
// T t = new T();
// // 获得此模型的公共属性
// PropertyInfo[] propertys = t.GetType().GetProperties();
// //遍历该对象的所有属性
// foreach (PropertyInfo pi in propertys)
// {
// tempName = pi.Name;//将属性名称赋值给临时变量
// //检查DataTable是否包含此列(列名==对象的属性名)
// if (dt.Columns.Contains(tempName))
// {
// // 判断此属性是否有Setter
// if (!pi.CanWrite) continue;//该属性不可写,直接跳出
// //取值
// object value = dr[tempName];
// //如果非空,则赋给对象的属性
// if (dr[pi.Name] is Int64)
// {
// pi.SetValue(t, Convert.ToInt32(dr[pi.Name]), null);
// continue;
// }
// if (value != DBNull.Value)
// pi.SetValue(t, value, null);
// }
// }
// //对象添加到泛型集合中
// ts.Add(t);
// }
// return ts;
//}
/// <summary>
/// 返回DataTable列名
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string[] GetColumnsByDataTable(System.Data.DataTable dt)
{
string[] strColumns = null;
if (dt.Columns.Count > 0)
{
int columnNum = 0;
columnNum = dt.Columns.Count;
strColumns = new string[columnNum];
for (int i = 0; i < dt.Columns.Count; i++)
{
strColumns[i] = dt.Columns[i].ColumnName;
}
}
return strColumns;
}
}
浙公网安备 33010602011771号