string columnName = rdr.GetName(i).ToUpper(); //不区分大小写
string columnRealName = rdr.GetName(i);
if (ht.ContainsKey(columnName) == false)
{
ht.Add(columnName, columnRealName);
}
}
return ht;
}
private static void SetValue(PropertyInfo propInfo, Object obj, object objValue)
{
try
{
propInfo.SetValue(obj, objValue, null);
}
catch
{
object realValue = null;
try
{
realValue = Convert.ChangeType(objValue, propInfo.PropertyType);
propInfo.SetValue(obj, realValue, null);
}
catch (Exception ex)
{
string err = ex.Message;
//throw ex; //在数据库数据有不符合规范的情况下应该及时抛出异常
}
}
}
#endregion
}
}
到这里,简单的数据访问持久化层就实现了。下面模仿楼猪使用的IBatis.net,写个伪SqlMapper,改善一下调用形式,丰富一下调用方法,让方法辨识度更高。
四、实现伪SqlMapper
1、BaseMapper类
代码
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
namespace AdoNetDataAccess.Mapper
{
using AdoNetDataAccess.Core.Contract;
public abstract class BaseMapper
{
public IDbOperation CurrentDbOperation;
#region query for list
public abstract IList<T> QueryForList<T>(string sqlStr)
where T : class, new();
public abstract IList<T> QueryForList<T>(string sqlStr, Type objType)
where T : class, new();
public abstract IList<T> QueryForList<T>(string sqlStr, CommandType cmdType, List<DbParameter> listParams)
where T : class, new();
public abstract IList<T> QueryForList<T>(string sqlStr, CommandType cmdType, List<DbParameter> listParams, Type objType)
where T : class, new();
#endregion
9/13 首页 上一页 7 8 9 10 11 12 下一页 尾页 |