RM.Common/DotNetData/ReaderToIListHelper.cs
(开头部分) 5KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Reflection;
namespace RM.Common.DotNetData
{
public class ReaderToIListHelper
{
public static DataTable DataTableToIDataReader(IDataReader reader)
{
DataTable objDataTable = new DataTable("Table");
int intFieldCount = reader.FieldCount;
for (int intCounter = 0; intCounter < intFieldCount; intCounter++)
{
objDataTable.Columns.Add(reader.GetName(intCounter).ToUpper(), reader.GetFieldType(intCounter));
}
objDataTable.BeginLoadData();
object[] objValues = new object[intFieldCount];
while (reader.Read())
{
reader.GetValues(objValues);
objDataTable.LoadDataRow(objValues, true);
}
reader.Close();
objDataTable.EndLoadData();
return objDataTable;
}
public static T ReaderToModel<T>(IDataReader dr)
{
T result;
try
{
try
{
if (dr.Read())
{
List<string> list = new List<string>(dr.FieldCount);
for (int i = 0; i < dr.FieldCount; i++)
{
list.Add(dr.GetName(i).ToLower());
}
T model = Activator.CreateInstance<T>();
PropertyInfo[] properties = model.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);
for (int j = 0; j < properties.Length; j++)
{
PropertyInfo pi = properties[j];
if (list.Contains(pi.Name.ToLower()))
{
if (!ReaderToIListHelper.IsNullOrDBNull(dr[pi.Name]))
{
pi.SetValue(model, ReaderToIListHelper.HackType(dr[pi.Name], pi.PropertyType), null);
}
}
}
result = model;
return result;
}
后面还有 80 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明
AI 解读
登录后可用,每次 10 积分,解读结果公开显示在下面。
还没有人解读过这个文件。
