/// <summary> /// Executes sql string and returns no result. /// </summary> /// <param name="sql">Sql string.</param> public void ExecuteNonQuery(string sql) { SWExecutor.ExecuteCommand(DB.Command, typeof(void), m_autoCloseConnection); }
/// <summary> /// Executes sql string and returns DataTable object. /// </summary> /// <param name="sql">Sql string.</param> /// <returns></returns> public DataTable ExecuteDataTable(string sql) { return((DataTable)SWExecutor.ExecuteCommand(DB.Command, typeof(DataTable), m_autoCloseConnection)); }
/// <summary> /// Executes sql string and returns scalar value. /// </summary> /// <param name="sql">Sql string.</param> /// <returns></returns> public object ExecuteScalar(string sql) { return(SWExecutor.ExecuteCommand(DB.Command, typeof(object), m_autoCloseConnection)); }
/// <summary> /// Generates an implementation to the method. /// </summary> /// <param name="ilGen">ILGenerator object.</param> /// <param name="RetType">Method's return type</param> /// <param name="prms">Method's parameters.</param> private static void GenerateMethodBody(ILGenerator ilGen, Type RetType, ParameterInfo [] prms) { /* * // declaring local variables * MethodInfo V_0; * Type V_1; * object[] V_2; * object V_3; * RetType V_4; * object[] V_5; */ LocalBuilder V_0 = ilGen.DeclareLocal(typeof(MethodInfo)); LocalBuilder V_1 = ilGen.DeclareLocal(typeof(Type)); LocalBuilder V_2 = ilGen.DeclareLocal(typeof(object[])); LocalBuilder V_3 = ilGen.DeclareLocal(typeof(object)); LocalBuilder V_4 = ilGen.DeclareLocal(RetType); LocalBuilder V_5 = ilGen.DeclareLocal(typeof(object[])); /* * V_0 = MethodBase.GetCurrentMethod(); */ MethodInfo miLocalMethod = typeof(MethodBase).GetMethod("GetCurrentMethod"); ilGen.Emit(OpCodes.Callvirt, miLocalMethod); ilGen.Emit(OpCodes.Castclass, typeof(MethodInfo)); ilGen.Emit(OpCodes.Stloc_S, V_0); /* * V_5 = new object[prms.Length]; */ ilGen.Emit(OpCodes.Ldc_I4, prms.Length); ilGen.Emit(OpCodes.Newarr, typeof(object)); ilGen.Emit(OpCodes.Stloc_S, V_5); for (int i = 0; i < prms.Length; ++i) { /* * V_5[i] = {method argument in position i+1}; */ ilGen.Emit(OpCodes.Ldloc_S, V_5); ilGen.Emit(OpCodes.Ldc_I4, i); ilGen.Emit(OpCodes.Ldarg_S, i + 1); // if method argument is a value type then box it if (prms[i].ParameterType.IsValueType) { ilGen.Emit(OpCodes.Box, prms[i].ParameterType); } else if (prms[i].ParameterType.IsByRef) { // get type from reference type Type reftype = SWExecutor.GetRefType(prms[i].ParameterType); // and box by this type if (reftype != null) { ilGen.Emit(OpCodes.Ldobj, reftype); if (reftype.IsValueType) { ilGen.Emit(OpCodes.Box, reftype); } } } ilGen.Emit(OpCodes.Stelem_Ref); } /* * V_5 = V_2; */ ilGen.Emit(OpCodes.Ldloc_S, V_5); ilGen.Emit(OpCodes.Stloc_S, V_2); /* * V_3 = SWExecutor.ExecuteMethodAndGetResult( * m_connection, * m_transaction, * V_0, * V_2 * ); */ // load this.m_connection ilGen.Emit(OpCodes.Ldarg_0); // this FieldInfo fldConnection = typeof(SqlWrapperBase).GetField("m_connection", BindingFlags.Instance | BindingFlags.NonPublic); ilGen.Emit(OpCodes.Ldfld, fldConnection); // load this.m_transaction ilGen.Emit(OpCodes.Ldarg_0); // this FieldInfo fldTransaction = typeof(SqlWrapperBase).GetField("m_transaction", BindingFlags.Instance | BindingFlags.NonPublic); ilGen.Emit(OpCodes.Ldfld, fldTransaction); // load V_0 ilGen.Emit(OpCodes.Ldloc_S, V_0); // load V_2 ilGen.Emit(OpCodes.Ldloc_S, V_2); // load this.m_autoCloseConnection ilGen.Emit(OpCodes.Ldarg_0); // this FieldInfo fldAutoCloseConnection = typeof(SqlWrapperBase).GetField("m_autoCloseConnection", BindingFlags.Instance | BindingFlags.NonPublic); ilGen.Emit(OpCodes.Ldfld, fldAutoCloseConnection); // call SWExecutor.ExecuteMethodAndGetResult methos MethodInfo miExecuteMethodAndGetResult = typeof(SWExecutor).GetMethod("ExecuteMethodAndGetResult", BindingFlags.Public | BindingFlags.Static); ilGen.Emit(OpCodes.Call, miExecuteMethodAndGetResult); // result -> V_3 ilGen.Emit(OpCodes.Stloc_S, V_3); // returning parameters passed by reference for (int i = 0; i < prms.Length; ++i) { if (prms[i].ParameterType.IsByRef) { Type reftype = SWExecutor.GetRefType(prms[i].ParameterType); if (reftype != null) { /* * {method argument in position i+1} = V_2[i] */ ilGen.Emit(OpCodes.Ldarg_S, i + 1); ilGen.Emit(OpCodes.Ldloc_S, V_2); ilGen.Emit(OpCodes.Ldc_I4, i); ilGen.Emit(OpCodes.Ldelem_Ref); if (reftype.IsValueType) { ilGen.Emit(OpCodes.Unbox, reftype); ilGen.Emit(OpCodes.Ldobj, reftype); } ilGen.Emit(OpCodes.Stobj, reftype); } } } if (RetType.FullName != "System.Void") { /* * return (RetType)V_3; */ ilGen.Emit(OpCodes.Ldloc_S, V_3); if (RetType.IsValueType) { ilGen.Emit(OpCodes.Unbox, RetType); ilGen.Emit(OpCodes.Ldobj, RetType); } else { ilGen.Emit(OpCodes.Castclass, RetType); } ilGen.Emit(OpCodes.Stloc_S, V_4); ilGen.Emit(OpCodes.Ldloc_S, V_4); } ilGen.Emit(OpCodes.Ret); }