欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DataReader 转换成实体(或List)

程序员文章站 2022-04-04 12:14:28
...

/// <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
相关标签: Mobile