/ Published in: C#
After having constructed a query, this piece of code can obtain the SQL statement before executing the query.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
private static Object GetClassInstance(Assembly assembly, String className, Object args) { Object result = null; if (assembly == null || String.IsNullOrEmpty(className)) return null; try { foreach (Type type in assembly.GetTypes()) { if (type.IsClass && type.FullName.EndsWith("." + className)) { if (args == null) { // Use the raw null value for the args parameter. A parameter // of type Object with a value of null causes an exception. // Also an empty list of parameters like Object args[] = new {}; // causes an exception. result = Activator.CreateInstance(type, null); } else result = Activator.CreateInstance(type, args); break; } } } catch { result = null; } return result; } private static String GetQuery(EntitySpaces.Interfaces.esDynamicQuery qry) { Assembly assembly; String sql = String.Empty; String className = "QueryBuilder"; try { qry.GetType().InvokeMember("PopulateRequest", BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance, null, qry, args); String assemblyName = qry.es.Connection.ProviderSignature.DataProviderName; assembly = Assembly.Load(assemblyName); Object qb = GetClassInstance(assembly, className, null); sql = qb.GetType().InvokeMember("BuildQuery", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.InvokeMethod, null, qb, args).ToString(); } catch { sql = null; } return sql; }