DataReader 转换成实体(或List)


/// <summary>
/// DataReader转换为obj list
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="rdr">datareader</param>
/// <returns>返回泛型类型</returns>
protected static IList<T> DataReader2Obj<T>(SqlDataReader rdr)
{
IList<T> list = new List<T>();

while (rdr.Read())
{
T t = System.Activator.CreateInstance<T>();
Type obj = t.GetType();
// 循环字段
for (int i = 0; i < rdr.FieldCount; i++)
{
object tempValue = null;

if (rdr.IsDBNull(i))
{

string typeFullName = obj.GetProperty(rdr.GetName(i)).PropertyType.FullName;
tempValue = GetDBNullValue(typeFullName);

}
else
{
tempValue = rdr.GetValue(i);

}

obj.GetProperty(rdr.GetName(i)).SetValue(t,tempValue, null);

}

list.Add(t);

}
return list;
}

/// <summary>
/// DataReader转换为obj
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="rdr">datareader</param>
/// <returns>返回泛型类型</returns>
protected static object DataReaderToObj<T>(SqlDataReader rdr)
{
T t = System.Activator.CreateInstance<T>();
Type obj = t.GetType();

if (rdr.Read())
{
// 循环字段
for (int i = 0; i < rdr.FieldCount; i++)
{
object tempValue = null;

if (rdr.IsDBNull(i))
{

string typeFullName = obj.GetProperty(rdr.GetName(i)).PropertyType.FullName;
tempValue = GetDBNullValue(typeFullName);

}
else
{
tempValue = rdr.GetValue(i);

}

obj.GetProperty(rdr.GetName(i)).SetValue(t, tempValue, null);

}
return t;
}
else
return null;

}


/// <summary>
/// 返回值为DBnull的默认值
/// </summary>
/// <param name="typeFullName">数据类型的全称,类如:system.int32</param>
/// <returns>返回的默认值</returns>
private static object GetDBNullValue(string typeFullName)
{

typeFullName = typeFullName.ToLower();

if (typeFullName == DataType.String)
{
return String.Empty;
}
if (typeFullName == DataType.Int32)
{
return 0;
}
if (typeFullName == DataType.DateTime)
{
return Convert.ToDateTime(BaseSet.DateTimeLongNull);
}
if (typeFullName == DataType.Boolean)
{
return false;
}
if (typeFullName == DataType.Int)
{
return 0;
}

return null;
}

#region = 返回集合使用例句 =

IList<MeetingsInfo> meetingInfoList = new List<MeetingsInfo>();

using (SqlDataReader rdr = SQLPlus.ExecuteReader(CommandType.Text, strSQL, parms))

{
meetingInfoList = DataReader2Obj<MeetingsInfo>(rdr);
}
#endregion

#region = 返回实体例句 =
MeetingsInfo meetingInfo = null;
using (SqlDataReader rdr = SQLPlus.ExecuteReader(CommandType.Text, strSQL, parms))
{
meetingInfo =(MeetingsInfo)DataReaderToObj<MeetingsInfo>(rdr);
}
#endregion

#region = 省略了类似如下内容 =
UsersInfo usersInfo = null;
using (SqlDataReader rdr = SQLPlus.ExecuteReader(CommandType.Text, strSQL, parms))
{
if (rdr.Read())
{
usersInfo = new UsersInfo();
usersInfo.UserID = rdr.IsDBNull(0) ? 0 : rdr.GetInt32(0);
usersInfo.UserName = rdr.IsDBNull(1) ? string.Empty : rdr.GetString(1);
usersInfo.Email = rdr.IsDBNull(2) ? string.Empty : rdr.GetString(2);
usersInfo.Mobile = rdr.IsDBNull(3) ? string.Empty : rdr.GetString(3);
usersInfo.RealName = rdr.IsDBNull(4) ? string.Empty : rdr.GetString(4);
usersInfo.CreateTime = rdr.IsDBNull(5) ? Convert.ToDateTime("0001-1-1 0:00:00") : rdr.GetDateTime(5);
usersInfo.LastLoginTime = rdr.IsDBNull(6) ? Convert.ToDateTime("0001-1-1 0:00:00") : rdr.GetDateTime(6);
usersInfo.LastUpdateTime = rdr.IsDBNull(7) ? Convert.ToDateTime("0001-1-1 0:00:00") : rdr.GetDateTime(7);
}

}
#endregion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值