public static void SetStructuredParameterValue <T>(this d.SqlClient.SqlCommand command, string parameterName, List <T> data) { var parameter = command.Parameters[parameterName]; if (parameter != null) { d.DataTable ret = new System.Data.DataTable(); var reference = Common.structureFields.Where(i => i.Item1.CommandText == command.CommandText && i.Item1.Connection.ConnectionString == command.Connection.ConnectionString && i.Item2 == parameter.TypeName).ToList(); foreach (var item in reference) { ret.Columns.Add(item.Item3); } foreach (var obj in data) { IBattleAxe ibattleAxe = (IBattleAxe)obj; d.DataRow row = ret.NewRow(); foreach (var item in reference) { var value = ibattleAxe[item.Item3]; if (value is Enum) { row[item.Item3] = (int)value; } else { row[item.Item3] = value; } } ret.Rows.Add(row); } parameter.Value = ret; } }
internal static d.DataTable GetDataTable(object referenceObject, d.SqlClient.SqlCommand command, d.SqlClient.SqlParameter parameter) { d.DataTable ret = new System.Data.DataTable(); if (referenceObject != null) { var reference = Common.structureFields.Where(i => i.Item1.CommandText == command.CommandText && i.Item1.Connection.ConnectionString == command.Connection.ConnectionString && i.Item2 == parameter.TypeName).ToList(); foreach (var item in reference) { ret.Columns.Add(item.Item3); } if (referenceObject is IBattleAxe) { var ibattleAxe = (IBattleAxe)referenceObject; d.DataRow row = ret.NewRow(); foreach (var item in reference) { row[item.Item3] = ibattleAxe[item.Item3]; } ret.Rows.Add(row); } else { var type = referenceObject.GetType(); if (type.Name == "List`1") { IList data = (IList)referenceObject; foreach (var obj in data) { IBattleAxe ibattleAxe = (IBattleAxe)obj; d.DataRow row = ret.NewRow(); foreach (var item in reference) { var value = ibattleAxe[item.Item3]; if (value is Enum) { row[item.Item3] = (int)value; } else { row[item.Item3] = value; } } ret.Rows.Add(row); } } } } return(ret); }
public static Func <T, string, object> GetMethod <T>(T sourceForInputParameters) where T : class { Func <T, string, object> getMethod; if (sourceForInputParameters is IBattleAxe) { getMethod = (obj, property) => { IBattleAxe temp = (IBattleAxe)obj; return(temp[property]); }; } else { GetValue tempGetMethod = getGetMethodFromInitialReflection(sourceForInputParameters); getMethod = (obj, property) => tempGetMethod(obj, property); } return(getMethod); }
/// <summary> /// the command should have the connections string set, doesnt have to be open but /// the string should be set. IBattleAxe assumes that the object is controlling /// all the value setting through the Indexer /// beware this has no error trapping so make sure to trap your errors /// </summary> /// <typeparam name="T"></typeparam> /// <param name="command"></param> /// <param name="parameter"></param> /// <returns></returns> public static T FirstOrDefault <T>(this d.SqlClient.SqlCommand command, IBattleAxe parameter) where T : class, IBattleAxe, new() { T newObj = null; try { if (command.IsConnectionOpen()) { setCommandParameterValues(parameter, command); newObj = getFirstFromReader <T>(command); setOutputParameters(parameter, command); } } catch (Exception) { throw; } finally { command.CloseConnection(); } return(newObj); }
/// <summary> /// the command should have the connections string set, doesnt have to be open but /// the string should be set. IBattleAxe assumes that the object is controlling /// all the value setting through the Indexer /// beware this has no error trapping so make sure to trap your errors /// </summary> /// <typeparam name="T">the return type</typeparam> /// <param name="command"></param> /// <param name="parameter">IBattle axe so can find parameter values from it using indexer</param> /// <returns></returns> public static List <T> ToList <T>(this d.SqlClient.SqlCommand command, IBattleAxe parameter) where T : class, IBattleAxe, new() { List <T> ret = new List <T>(); try { if (command.IsConnectionOpen()) { setCommandParameterValues(parameter, command); executeReaderAndFillList(command, ret); setOutputParameters(parameter, command); } } catch (Exception) { throw; } finally { command.CloseConnection(); } return(ret); }