/// <summary> /// Selects the specified command. /// </summary> /// <param name="command">The command.</param> /// <returns></returns> public static DataAccessView <T> Select <T>(string command) { Type type = typeof(T); DbCommandAttribute attribute = ObjectHelper.FindCommand(type, command); using (DataAccess db = new DataAccess(attribute.CommandText)) { db.CommandType = attribute.CommandType; using (IDataReader reader = db.ExecuteReader()) { return(FetchCollection <T>(reader)); } } }
/// <summary> /// /// </summary> /// <param name="type"></param> /// <param name="command"></param> /// <returns></returns> public static DbCommandAttribute FindCommand(Type type, string command) { DbCommandAttribute commandAttribute = null; foreach (Attribute attribute in TypeDescriptor.GetAttributes(type)) { commandAttribute = attribute as DbCommandAttribute; if (commandAttribute != null && commandAttribute.CommandName == command) { return(commandAttribute); } } throw CommandNotExists(command, type); }
/// <summary> /// Deletes the specified data object. /// </summary> /// <param name="dataObject">The data object.</param> /// <param name="command">The command.</param> /// <returns></returns> public static int Delete <T>(T dataObject, string command) { Type type = dataObject.GetType(); DbCommandAttribute attribute = ObjectHelper.FindCommand(type, command); using (DataAccess db = new DataAccess(attribute.CommandText)) { db.CommandType = attribute.CommandType; foreach (string parameterName in attribute.Parameters) { db.AddParameter(parameterName, ObjectHelper.GetParameterValue(dataObject, parameterName)); } return(db.ExecuteNonQuery()); } }
/// <summary> /// Inserts the specified data object. /// </summary> /// <param name="dataObject">The data object.</param> /// <param name="command">The command.</param> /// <returns></returns> public static T Insert <T>(T dataObject, string command) { Type type = dataObject.GetType(); DbCommandAttribute attribute = ObjectHelper.FindCommand(type, command); using (DataAccess db = new DataAccess(attribute.CommandText)) { db.CommandType = attribute.CommandType; foreach (string parameterName in attribute.Parameters) { db.AddParameter(parameterName, ObjectHelper.GetParameterValue(dataObject, parameterName)); } dataObject = db.FetchObject <T>(); } return(dataObject); }