///
///dataReader转换工具///
public classDataReaderUtils
{///
///放到最后 转换///
///
///
///
public static List DataReadertoList(MySqlDataReader DataReader) where T : new()
{
List t_lis = new List();if (DataReader != null)
{if (!DataReader.IsClosed)
{//暂未关闭
while(DataReader.Read())
{
List DataReaderColumnNames =ColumnNames(DataReader);
T t= newT();
Type type=t.GetType();
PropertyInfo[] p_list=type.GetProperties();
FieldInfo[] fieldInfos=type.GetFields();if (p_list != null && p_list.Length > 0)
{foreach (var i inp_list)
{
Attribute datareaderattr= i.GetCustomAttribute(typeof(DataReaderModelAttribute), false);if (datareaderattr != null)
{
DataReaderModelAttribute dataattr=(DataReaderModelAttribute)datareaderattr;
DataReader.GetSchemaTable().DefaultView.RowFilter= "ColumnName= '" + dataattr.Datareaderfieldname + "'";if (!DataReaderColumnNames.Contains(dataattr.Datareaderfieldname))
{continue;
}if(DataReader[dataattr.Datareaderfieldname].Equals(DBNull.Value))
{//如果是dbnull
continue;
}if (DataReader[dataattr.Datareaderfieldname].GetType().Name == "Int32")
{int vals =Convert.ToInt32(DataReader[dataattr.Datareaderfieldname]);
i.SetValue(t, vals,null);
}if (DataReader[dataattr.Datareaderfieldname].GetType().Name == "Int64")
{int vals =Convert.ToInt32(DataReader[dataattr.Datareaderfieldname]);
i.SetValue(t, vals,null);
}else{
i.SetValue(t, DataReader[dataattr.Datareaderfieldname],null);
}
}else{continue;
}
}
t_lis.Add(t);
}else{if (fieldInfos != null && fieldInfos.Length > 0)
{foreach (var i infieldInfos)
{
Attribute datareaderattr= i.GetCustomAttribute(typeof(DataReaderModelAttribute), false);if (datareaderattr != null)
{
DataReaderModelAttribute dataattr=(DataReaderModelAttribute)datareaderattr;
DataReader.GetSchemaTable().DefaultView.RowFilter= "ColumnName= '" + dataattr.Datareaderfieldname + "'";if (!DataReaderColumnNames.Contains(dataattr.Datareaderfieldname))
{continue;
}if(DataReader[dataattr.Datareaderfieldname].Equals(DBNull.Value))
{//如果是dbnull
continue;
}if (DataReader[dataattr.Datareaderfieldname].GetType().Name == "Int32")
{int vals =Convert.ToInt32(DataReader[dataattr.Datareaderfieldname]);
i.SetValue(t, vals);
}if (DataReader[dataattr.Datareaderfieldname].GetType().Name == "Int64")
{int vals =Convert.ToInt32(DataReader[dataattr.Datareaderfieldname]);
i.SetValue(t, vals);
}else{
i.SetValue(t, DataReader[dataattr.Datareaderfieldname]);
}
}else{continue;
}
}
t_lis.Add(t);
}//DataReader.Close();//break;
}
}//DataReader.Close();
returnt_lis;
}else{//关闭
returnt_lis;
}
}else{returnt_lis;
}
}///
///获取datareader 列字段名称集合///
///
///
private static ListColumnNames(MySqlDataReader pReader)
{
List Columnnames = new List();for (int i = 0; i < pReader.FieldCount; i++)
{
Columnnames.Add(pReader.GetName(i));
}returnColumnnames;
}///
///datareader to class object datareader 转成 一个对象///
///
///
public static T DataReaderToClass(MySqlDataReader datareader) where T : class, new()
{
T t= newT();
Type type=t.GetType();if (datareader != null)
{
PropertyInfo[] infos=type.GetProperties();
FieldInfo[] fieldInfos=type.GetFields();if (infos != null && infos.Length > 0)
{if(datareader.Read())
{
List DataReaderColumnNames =ColumnNames(datareader);foreach (var i ininfos)
{
Attribute attr= i.GetCustomAttribute(typeof(DataReaderModelAttribute));if (attr != null)
{
DataReaderModelAttribute dataattr=(DataReaderModelAttribute)attr;if (!DataReaderColumnNames.Contains(dataattr.Datareaderfieldname))
{continue;
}if(datareader[dataattr.Datareaderfieldname].Equals(DBNull.Value))
{//如果是dbnull
continue;
}if (!datareader[dataattr.Datareaderfieldname].Equals(DBNull.Value))
{
i.SetValue(t, datareader[dataattr.Datareaderfieldname]);
}else{continue;
}
}else{continue;
}
}returnt;
}else{return null;
}
}else{if (fieldInfos != null && fieldInfos.Length > 0)
{if(datareader.Read())
{
List DataReaderColumnNames =ColumnNames(datareader);foreach (var i infieldInfos)
{
Attribute attr= i.GetCustomAttribute(typeof(DataReaderModelAttribute));if (attr != null)
{
DataReaderModelAttribute dataattr=(DataReaderModelAttribute)attr;if (!DataReaderColumnNames.Contains(dataattr.Datareaderfieldname))
{continue;
}if(datareader[dataattr.Datareaderfieldname].Equals(DBNull.Value))
{//如果是dbnull
continue;
}if (!datareader[dataattr.Datareaderfieldname].Equals(DBNull.Value))
{
i.SetValue(t, datareader[dataattr.Datareaderfieldname]);
}else{continue;
}
}else{continue;
}
}returnt;
}else{return null;
}
}else{return null;
}
}
}else{return null;
}
}
}