public static T GetModel<T>(T model, string wherestr) where T : new()
{
DBHelp db = new DBHelp();
string typeName = model.GetType().Name;
typeName = typeName.Substring(0, typeName.Length - 4);
string strF = DbFields.FieldsBuilingInfo;
string sql = string.Format("select {0} from {1} where {2}", strF, typeName, wherestr);
PropertyInfo[] propertys = model.GetType().GetProperties();
Hashtable dt = db.GetRecord(sql);
foreach (PropertyInfo property in propertys)
{
string str = property.Name.ToLower();
foreach (DictionaryEntry de in dt)
{
//注意HastTable内存储的默认类型是object,需要进行转换才可以输出
string t = de.Value.GetType().ToString();
if (de.Key.ToString().ToLower() == str)
{
if (de.Value.GetType().Equals(typeof(String)))
{
property.SetValue(model, de.Value.ToString(), null);
break;
}
else if (de.Value.GetType().Equals(typeof(DateTime)))
{
property.SetValue(model, DateTime.Parse(de.Value.ToString()), null);
break;
}
else if (de.Value.GetType().Equals(typeof(byte)))
{
property.SetValue(model, byte.Parse(de.Value.ToString()), null);
break;
}
else if (de.Value.GetType().Equals(typeof(short)))
{
property.SetValue(model, short.Parse(de.Value.ToString()), null);
break;
}
else if (de.Value.GetType().Equals(typeof(int)))
{
property.SetValue(model, int.Parse(de.Value.ToString()), null);
break;
}
else if (de.Value.GetType().Equals(typeof(float)))
{
property.SetValue(model, float.Parse(de.Value.ToString()), null);
break;
}
else if (de.Value.GetType().Equals(typeof(double)))
{
property.SetValue(model, System.Convert.ToDouble(de.Value.ToString()), null);
break;
}
else if (de.Value.GetType().Equals(typeof(decimal)))
{
property.SetValue(model, System.Convert.ToDecimal(de.Value.ToString()), null);
break;
}
else if (de.Value.GetType().Equals(typeof(Single)))
{
property.SetValue(model, System.Convert.ToSingle(de.Value.ToString()), null);
break;
}
}
}
//}
}
return model;
}
public static T GetModel2<T>(T model, string wherestr, string strField) where T : new()
{
DBHelp db = new DBHelp();
string typeName = model.GetType().Name;
typeName = typeName.Substring(0, typeName.Length - 4);
string sql = string.Format("select {0} from {1} where {2}", strField, typeName, wherestr);
PropertyInfo[] propertys = model.GetType().GetProperties();
Hashtable dt = db.GetRecord(sql);
try
{
foreach (PropertyInfo property in propertys)
{
string str = property.Name.ToLower();
foreach (DictionaryEntry de in dt)
{
//注意HastTable内存储的默认类型是object,需要进行转换才可以输出
if (de.Key.ToString().ToLower() == str)
{
if (de.Value.GetType().Equals(typeof(String)))
{
property.SetValue(model, de.Value.ToString(), null);
}
else if (de.Value.GetType().Equals(typeof(bool)))
{
property.SetValue(model, bool.Parse(de.Value.ToString()), null);
}
else if (de.Value.GetType().Equals(typeof(DateTime)))
{
property.SetValue(model, de.Value, null);
}
else if (de.Value.GetType().Equals(typeof(byte)))
{
property.SetValue(model, byte.Parse(de.Value.ToString()), null);
}
else if (de.Value.GetType().Equals(typeof(short)))
{
property.SetValue(model, short.Parse(de.Value.ToString()), null);
}
else if (de.Value.GetType().Equals(typeof(int)))
{
property.SetValue(model, int.Parse(de.Value.ToString()), null);
}
else if (de.Value.GetType().Equals(typeof(float)))
{
property.SetValue(model, float.Parse(de.Value.ToString()), null);
}
else if (de.Value.GetType().Equals(typeof(double)))
{
property.SetValue(model, double.Parse(de.Value.ToString()), null);
}
else if (de.Value.GetType().Equals(typeof(decimal)))
{
property.SetValue(model, System.Convert.ToDecimal(de.Value.ToString()), null);
break;
}
else if (de.Value.GetType().Equals(typeof(Single)))
{
property.SetValue(model, System.Convert.ToSingle(de.Value.ToString()), null);
break;
}
}
}
}
}
catch(Exception ex){}
return model;
}
coolly
专注于企业信息化
技术领域:MOSS,ASP.net,SQLServer
目前行业:房地产,重庆商用物业
Email:1001tao@gmail.com
Blog:www.cnblogs.com/1001tao