从数据库中读取数据行,生成一个泛型对象,把数据填充进泛型对象,泛型可以带参数。
与Dapper的Query<T>效果差不多。
/// <summary>
/// 从数据库读取,生成一个泛型对象,读取字段值填充进去
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="sql">查询脚本</param>
/// <param name="command">命令组件</param>
/// <param name="T_CreateParams">泛型T创建时的参数,默认无参数</param>
/// <returns>新生成的泛型对象</returns>
public static T getSqlResult<T>(string sql, OleDbCommand command, params object[] T_CreateParams)
{
T result = default(T);
DataRow db = null;
try
{
db = getSqlResult(sql, command); //传入sql,返回dataRow
if (db == null) return default(T);
//result = System.Activator.CreateInstance<T>();
result = (T)System.Activator.CreateInstance(typeof(T), T_CreateParams);
Model.__model.loadData(result, db);
}
catch (Exception x)
{ throw; }
return result;
}
//Model中的类:
public static gsDefineTypes.ResponsMessage loadData(object obj, DataRow db, params object[] args)
{ //从属性中直接复制,参考Gs_Class.getComponentEventDelegates
gsDefineTypes.ResponsMessage result = new gsDefineTypes.ResponsMessage();
string s = "", sField = "";
Type tTo = null;
double n = 0;
//System.Reflection.FieldInfo[] list = objFrom.GetType().GetFields();
//foreach (System.Reflection.FieldInfo itm in list)
try
{
tTo = obj.GetType();
//循环所有列
foreach (DataColumn itm in db.Table.Columns)
{
s += (s == "" ? "" : "\r\n") + (sField = itm.ColumnName);
object o = db[sField];
if (o == null || o is DBNull) continue;
//数字型都转成double,GsDefineTypes.isNumber(o.GetType())
if (o is decimal) o = Convert.ToDouble(o);
//字段{get;set;}的
System.Reflection.FieldInfo f = tTo.GetField(sField);
if (f != null)
{ //字段
o = tryConvertType(o, f.FieldType); //逻辑型和数字型的转换
f.SetValue(obj, o); continue;
}
//属性
PropertyInfo pi = tTo.GetProperty(sField);
if (pi != null)
{
o = tryConvertType(o, pi.PropertyType); //逻辑型和数字型的转换
pi.SetValue(obj, o, null); continue;
}
//iLog.Info(GetType().ToString(), "loadData:" + sField + " = " + o.ToString());
}
//iLog.Info(GetType().ToString(), "loadData:"+s);
result.setOkMark();
}
catch (Exception x)
{
loger.Info(tTo.ToString(), s = tTo.ToString() + ".loadData-" + sField + "错误:" + x.Message + "");
throw new Exception(s);
}
return result;
}
public static object tryConvertType(object o, Type toType)
{
if (o == null) return o;
object result = o;
try
{
if (toType == typeof(bool)) result = Convert.ToBoolean(o);
else if (toType == typeof(int))
{
if (o is Boolean) result = (Convert.ToBoolean(o) ? 1 : 0);
else if (o is decimal || o is double) result = Math.Round(Convert.ToDouble(o));
else if (o is string) result = Math.Round(Convert.ToDouble(o));
else result = Convert.ToInt32(o);
}
else if (toType == typeof(double))
{
if (o.GetType() != toType) result = Convert.ToDouble(o);
}
else if (toType == typeof(DateTime)) result = Convert.ToDateTime(o);
else if (toType == typeof(string))
{
if (o.GetType() != toType) result = Convert.ToString(o);
}
}
catch (Exception x)
{ }
return result;
}
用法:
public void a(string sUser)
{
_logUsers usr = iData.getSqlResult<_logUsers>("select * from logUsers where sID='" + sUser + "'", cmd);
if (usr != null) showInfo("当前用户:" + usr.sID + "." + usr.sName);
}
public class _logUsers
{ //用户类
public string sID = "";
public string sName = "";
}
泛型的创建:T result=(T)System.Activator.CreateInstance(typeof(T), T_CreateParams);