//分页select * from (select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from tb_User) as temp where rowNumber between (((@pageindex-1)*@pagesize)+1) and (@pageindex*@pagesize) public IEnumerable <T> FindPageList <T>(string where, int pageIndex, int pageSize, out int total, string sort = "") where T : new() { map = GetDataMap <T>(); DBProviderHelper db = new DBProviderHelper(); var jointSql = dbprovider.JointSql(map.tableName, map.GetColumnMapListStr(), where, sort); var start = ((pageIndex - 1) * pageSize) + 1; var end = (pageIndex * pageSize); string querySql = dbprovider.GetPageListSql(jointSql); DbParameter[] param = dbprovider.GetParameter(start, end, pageSize); //先获取记录总数 total = GetCount("select count(1) from " + map.tableName + " " + where); return(this.QueryCommandList <T>(querySql, param)); }
public static void Add(Type t) { DataMap map = null; TableAttribute tableAttribute = null; var tableAttributeObject = t.GetCustomAttributes(typeof(TableAttribute), true).FirstOrDefault(); if (tableAttributeObject.IsNullOrSpace()) { map = new DataMap(t.Name, t.Name, t); } else { tableAttribute = (TableAttribute)tableAttributeObject; map = new DataMap(tableAttribute.tableName, t.Name, t); } maps.Add(t, map); }
public static TResult Load <TResult>(TResult model, IDataReader reader) where TResult : new() { Type t = typeof(TResult); PropertyInfo[] properties = t.GetProperties(); DataMap map = new DataMap(t.Name, t.Name, t); foreach (PropertyInfo info in properties) { if (reader[info.Name].GetType() == typeof(Int64)) { info.SetValue(model, Convert.ToInt32(reader[info.Name])); continue; } } return(model); }
public List <TResult> QueryReflect <TResult>(string querySql, object paramters = null) where TResult : new() { var list = new List <TResult>(); map = GetDataMap <TResult>(); //反射获取参数值 List <DbParameter> param = new List <DbParameter>(); CreateParamster(paramters, ref param); var reader = QueryCommandReader(querySql, param.ToArray()); if (reader.HasRows) { while (reader.Read()) { list.Add(Load(new TResult(), reader)); } } reader.Close(); return(list); }
private static Func <object, string> CreateInsertMethodImpl(object model) { Type type = model.GetType(); DynamicMethod method = new DynamicMethod(string.Format("Update{0}", Guid.NewGuid()), typeof(string), new Type[] { typeof(object) }, true); var iLGenerator = method.GetILGenerator(); iLGenerator.DeclareLocal(typeof(StringBuilder)); iLGenerator.Emit(OpCodes.Newobj, typeof(StringBuilder).GetConstructor(new Type[] { })); iLGenerator.Emit(OpCodes.Stloc_0); iLGenerator.DeclareLocal(typeof(StringBuilder)); iLGenerator.Emit(OpCodes.Newobj, typeof(StringBuilder).GetConstructor(new Type[] { })); iLGenerator.Emit(OpCodes.Stloc_1); DataMap map = Metadata.GetDataMap(model.GetType()); iLGenerator.BeginExceptionBlock(); var indexlocal = iLGenerator.DeclareLocal(typeof(string)); var indexlocalInt32 = iLGenerator.DeclareLocal(typeof(Int32)); var indexlocalSecond = iLGenerator.DeclareLocal(typeof(string)); foreach (ColumnMap item in (from p in map._properties select(map.GetMember(p.Name)))) { if (!item.isAutoIncrement) { iLGenerator.Emit(OpCodes.Ldarg_0); iLGenerator.Emit(OpCodes.Unbox_Any, type); iLGenerator.Emit(OpCodes.Callvirt, item.GetMethodInfo); if (item.MemberType == typeof(Int32)) { iLGenerator.Emit(OpCodes.Stloc, indexlocalInt32); iLGenerator.Emit(OpCodes.Ldloca_S, indexlocalInt32); } if (item.MemberType == typeof(Boolean)) { iLGenerator.Emit(OpCodes.Call, typeof(Convert).GetMethod("ToInt32", new Type[] { typeof(Boolean) })); iLGenerator.Emit(OpCodes.Stloc, indexlocalInt32); iLGenerator.Emit(OpCodes.Ldloca_S, indexlocalInt32); } if (item.MemberType == typeof(Boolean)) { iLGenerator.Emit(OpCodes.Call, typeof(Int32).GetMethod("ToString", new Type[] { })); } else { iLGenerator.Emit(OpCodes.Call, item.MemberType.GetMethod("ToString", new Type[] { })); } iLGenerator.Emit(OpCodes.Stloc, indexlocal); iLGenerator.Emit(OpCodes.Ldstr, CreateFormatParamByType(item.MemberType)); iLGenerator.Emit(OpCodes.Ldloc, indexlocal); iLGenerator.Emit(OpCodes.Call, typeof(string).GetMethod("Format", new Type[] { typeof(string), item.MemberType })); iLGenerator.Emit(OpCodes.Stloc, indexlocal); iLGenerator.Emit(OpCodes.Ldloc_0); iLGenerator.Emit(OpCodes.Ldloc, indexlocal); iLGenerator.Emit(OpCodes.Callvirt, typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(string) })); iLGenerator.Emit(OpCodes.Stloc_0); iLGenerator.Emit(OpCodes.Ldstr, "{0},"); iLGenerator.Emit(OpCodes.Ldstr, item.MemberName); iLGenerator.Emit(OpCodes.Call, typeof(string).GetMethod("Format", new Type[] { typeof(string), item.MemberType })); iLGenerator.Emit(OpCodes.Stloc, indexlocalSecond); iLGenerator.Emit(OpCodes.Ldloc_1); iLGenerator.Emit(OpCodes.Ldloc, indexlocalSecond); iLGenerator.Emit(OpCodes.Callvirt, typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(string) })); iLGenerator.Emit(OpCodes.Stloc_1); } } iLGenerator.BeginCatchBlock(typeof(Exception)); iLGenerator.EmitCall(OpCodes.Call, typeof(Check).GetMethod("ThrowDataException"), null); iLGenerator.EndExceptionBlock(); iLGenerator.Emit(OpCodes.Ldloc_0); iLGenerator.Emit(OpCodes.Callvirt, typeof(StringBuilder).GetMethod("ToString", new Type[] { })); iLGenerator.Emit(OpCodes.Call, typeof(SqlMapper).GetMethod("RemoveComma", new Type[] { typeof(string) })); iLGenerator.Emit(OpCodes.Stloc, indexlocal); iLGenerator.Emit(OpCodes.Ldloc_1); iLGenerator.Emit(OpCodes.Callvirt, typeof(StringBuilder).GetMethod("ToString", new Type[] { })); iLGenerator.Emit(OpCodes.Call, typeof(SqlMapper).GetMethod("RemoveComma", new Type[] { typeof(string) })); iLGenerator.Emit(OpCodes.Stloc, indexlocalSecond); iLGenerator.Emit(OpCodes.Ldstr, "INSERT INTO {0} ({1}) VALUES ({2})"); iLGenerator.Emit(OpCodes.Ldstr, map.tableName); iLGenerator.Emit(OpCodes.Ldloc, indexlocalSecond); iLGenerator.Emit(OpCodes.Ldloc, indexlocal); iLGenerator.Emit(OpCodes.Call, typeof(string).GetMethod("Format", new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) })); iLGenerator.Emit(OpCodes.Ret); return((Func <object, string>)method.CreateDelegate(typeof(Func <object, string>))); }
public static Func <object, string> CreateUpdateSqlMethodImpl(object model) { Type type = model.GetType(); DynamicMethod method = new DynamicMethod(string.Format("Update{0}", Guid.NewGuid()), typeof(string), new Type[] { typeof(object) }, true); var iLGenerator = method.GetILGenerator(); iLGenerator.DeclareLocal(typeof(StringBuilder)); iLGenerator.Emit(OpCodes.Newobj, typeof(StringBuilder).GetConstructor(new Type[] { })); iLGenerator.Emit(OpCodes.Stloc_0); DataMap map = Metadata.GetDataMap(model.GetType()); iLGenerator.Emit(OpCodes.Ldloc_0); iLGenerator.Emit(OpCodes.Ldstr, "update " + map.tableName + " set "); iLGenerator.Emit(OpCodes.Callvirt, typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(string) })); iLGenerator.Emit(OpCodes.Stloc_0); iLGenerator.BeginExceptionBlock(); var indexlocal = iLGenerator.DeclareLocal(typeof(string)); var indexlocalInt32 = iLGenerator.DeclareLocal(typeof(Int32)); foreach (ColumnMap item in (from p in map._properties select(map.GetMember(p.Name)))) { if (!item.isPrimaryKey) { iLGenerator.Emit(OpCodes.Ldarg_0); iLGenerator.Emit(OpCodes.Unbox_Any, type); iLGenerator.Emit(OpCodes.Callvirt, item.GetMethodInfo); if (item.MemberType == typeof(Int32)) { iLGenerator.Emit(OpCodes.Stloc, indexlocalInt32); iLGenerator.Emit(OpCodes.Ldloca_S, indexlocalInt32); } if (item.MemberType == typeof(Boolean)) { iLGenerator.Emit(OpCodes.Call, typeof(Convert).GetMethod("ToInt32", new Type[] { typeof(Boolean) })); iLGenerator.Emit(OpCodes.Stloc, indexlocalInt32); iLGenerator.Emit(OpCodes.Ldloca_S, indexlocalInt32); } if (item.MemberType == typeof(Boolean)) { iLGenerator.Emit(OpCodes.Call, typeof(Int32).GetMethod("ToString", new Type[] { })); } else { iLGenerator.Emit(OpCodes.Call, item.MemberType.GetMethod("ToString", new Type[] { })); } iLGenerator.Emit(OpCodes.Stloc, indexlocal); iLGenerator.Emit(OpCodes.Ldstr, item.MemberName + (item.MemberType == typeof(Int32) || item.MemberType == typeof(Boolean)?"=":"='")); iLGenerator.Emit(OpCodes.Ldloc, indexlocal); iLGenerator.Emit(OpCodes.Ldstr, item.MemberType == typeof(Int32) || item.MemberType == typeof(Boolean)?",":"',"); iLGenerator.Emit(OpCodes.Call, typeof(string).GetMethod("Concat", new Type[] { typeof(object), typeof(object), typeof(object) })); iLGenerator.Emit(OpCodes.Stloc, indexlocal); iLGenerator.Emit(OpCodes.Ldloc_0); iLGenerator.Emit(OpCodes.Ldloc, indexlocal); iLGenerator.Emit(OpCodes.Callvirt, typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(string) })); iLGenerator.Emit(OpCodes.Stloc_0); } } iLGenerator.BeginCatchBlock(typeof(Exception)); iLGenerator.EmitCall(OpCodes.Call, typeof(Check).GetMethod("ThrowDataException"), null); iLGenerator.EndExceptionBlock(); iLGenerator.Emit(OpCodes.Ldloc_0); iLGenerator.Emit(OpCodes.Callvirt, typeof(StringBuilder).GetMethod("ToString", new Type[] { })); iLGenerator.Emit(OpCodes.Call, typeof(SqlMapper).GetMethod("RemoveComma", new Type[] { typeof(string) })); iLGenerator.Emit(OpCodes.Ret); return((Func <object, string>)method.CreateDelegate(typeof(Func <object, string>))); }