/// <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> /// <param name="command"></param> /// <param name="objs"></param> /// <returns></returns> public static List <T> Update <T>(this d.SqlClient.SqlCommand command, List <T> objs) where T : class, IBattleAxe { try { if (command.IsConnectionOpen()) { foreach (var obj in objs) { setCommandParameterValues(obj, command); command.ExecuteNonQuery(); setOutputParameters(obj, command); } } command.CloseConnection(); } catch (Exception) { throw; } finally { command.CloseConnection(); } return(objs); }
/// <summary> /// the command should have the connections string set, doesnt have to be open but /// the string should be set. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="command"></param> /// <param name="parameter"></param> /// <returns></returns> public static List <T> ToList <T>(this d.SqlClient.SqlCommand command, T parameter = null) where T : class, new() { List <T> ret = new List <T>(); try { if (command.IsConnectionOpen()) { setCommandParameters(parameter, command); using (var reader = command.ExecuteReader()) { var map = DataReaderMap.GetReaderMap(reader); while (reader.Read()) { T newObj = new T(); DataReaderMap.Set(reader, map, newObj); ret.Add(newObj); } } setOutputParameters(parameter, command); } } catch { throw; } finally { command.CloseConnection(); } return(ret); }
/// <summary> /// execute a string back to the database using group names from regex to supply to the stored procedures /// </summary> /// <typeparam name="T"></typeparam> /// <param name="command"></param> /// <param name="source">raw line that will be parsed via regex</param> /// <param name="regex"></param> /// <param name="regexNotFoundOrValueIsNull">if the parameter cannot find group this is method called for the value of the parameter</param> /// <returns></returns> public static T ExecuteWithRegex <T>(this d.SqlClient.SqlCommand command, string source, System.Text.RegularExpressions.Regex regex, Func <string, object> regexNotFoundOrValueIsNull = null) where T : class, IBattleAxe, new() { var outputParameters = new T(); if (regex.TrySetSqlCommandParameterValues(source, command, regexNotFoundOrValueIsNull)) { try { if (command.IsConnectionOpen()) { command.ExecuteNonQuery(); command.Connection.Close(); foreach (d.IDbDataParameter parameter in command.Parameters) { if (parameter.Direction == d.ParameterDirection.InputOutput || parameter.Direction == d.ParameterDirection.Output) { var field = parameter.ParameterName.Replace("@", ""); outputParameters[field] = parameter.Value; } } } } catch (Exception ex) { string formatted = string.Format("Execution of 'ExecuteWithRegex' SqlCommand:{0}, ErrorMessage:{1}", command.CommandText, ex.Message); throw new Exception(formatted); } finally { command.Connection.Close(); } } else { string formatted = string.Format("Failed to 'ExecuteWithRegex' SqlCommand:{0}", command.CommandText); throw new Exception(formatted); } return(outputParameters); }
/// <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> /// <param name="command"></param> /// <param name="obj"></param> /// <returns></returns> public static T Execute <T>(this d.SqlClient.SqlCommand command, T obj) where T : class, IBattleAxe, new() { try { if (command.IsConnectionOpen()) { setCommandParameterValues(obj, command); command.ExecuteNonQuery(); setOutputParameters(obj, command); } } catch (Exception) { throw; } finally { command.CloseConnection(); } return(obj); }
/// <summary> /// the command should have the connections string set, doesnt have to be open but /// the string should be set. /// beware this has no error trapping so make sure to trap your errors /// </summary> /// <typeparam name="T"></typeparam> /// <param name="command"></param> /// <returns></returns> public static List <T> ToList <T>(this d.SqlClient.SqlCommand command) where T : class, IBattleAxe, new() { List <T> ret = new List <T>(); try { if (command.IsConnectionOpen()) { executeReaderAndFillList(command, ret); } } catch (Exception) { throw; } finally { command.CloseConnection(); } return(ret); }
/// <summary> /// the command should have the connections string set, doesnt have to be open but /// the string should be set. /// </summary> /// <param name="command"></param> /// <param name="obj"></param> /// <returns></returns> public static T Execute <T>(this d.SqlClient.SqlCommand command, T obj = null) where T : class { try { setCommandParameters(obj, command); if (command.IsConnectionOpen()) { command.ExecuteNonQuery(); setOutputParameters(obj, command); } command.Connection.Close(); } catch (Exception) { throw; } finally { command.Connection.Close(); } return(obj); }
/// <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); }
/// <summary> /// the command should have the connections string set, doesnt have to be open but /// the string should be set. /// </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, T parameter = null) where T : class, new() { T newObj = null; try { if (command.IsConnectionOpen()) { setCommandParameters(parameter, command); newObj = getFirstFromDataReader <T>(command); setOutputParameters(parameter, command); } } catch { throw; } finally { command.CloseConnection(); } return(newObj); }