using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace CommonTools.Database { public class ColumnData { public class ColumnDataInfo { public String DataType; public Boolean TypeIsString; public String Name; public override string ToString() { return Name; } } /// /// Retrieve the "ColName" attribute from an attribute. /// /// The "real" column name. /// The table enumeration value /// The column enumeration value public static String GetName(Enum tableEnumValue, Enum columnEnumValue) { return tableEnumValue.ToString() + "." + GetName(columnEnumValue); } /// /// Retrieve the "ColName" attribute from an attribute. /// /// The "real" column name. /// The enumeration value public static String GetName(Enum enumValue) { string columnName = ""; Type type = enumValue.GetType(); MemberInfo[] memberInfo = type.GetMember(enumValue.ToString()); if (memberInfo != null && memberInfo.Length == 1) { object[] attributes = memberInfo[0].GetCustomAttributes(typeof(ColName), false); if (attributes.Length == 1) { columnName = ((ColName)attributes[0]).Text; } } return columnName; } /// /// Retrieve the DataType attribute from an attribute. /// /// The DataType of the column. /// The enumeration value public static String GetDataType(Enum enumValue) { string dataType = ""; Type type = enumValue.GetType(); MemberInfo[] memberInfo = type.GetMember(enumValue.ToString()); if (memberInfo != null && memberInfo.Length == 1) { object[] attributes = memberInfo[0].GetCustomAttributes(typeof(DataType), false); if (attributes.Length == 1) { dataType = ((DataType)attributes[0]).Text; } } return dataType; } /// /// Whether the DataType attribute is a String: char, longtext, text, varchar. /// /// True when the DataType is a String, false otherwhise /// The enumeration value public static Boolean IsDataTypeString(Enum enumValue) { String dataType = GetDataType(enumValue); return dataType.CompareTo("char") == 0 || dataType.CompareTo("longtext") == 0 || dataType.CompareTo("text") == 0 || dataType.CompareTo("varchar") == 0; } /// /// Retrieve the String value of the "ColName" attribute from an attribute. /// /// The String value of the column name. /// The data row /// The enumeration value public static String GetString(System.Data.DataRow dataRow, Enum enumValue) { string columnName = GetName(enumValue); return dataRow[columnName].ToString(); } /// /// Retrieve the Int32 value of the "ColName" attribute from an attribute. /// /// The Int32 value of the column name. /// The data row /// The enumeration value public static Int32 GetInt32(System.Data.DataRow dataRow, Enum enumValue) { string columnName = GetName(enumValue); String tableName = dataRow.Table.TableName; return System.Int32.Parse(dataRow[columnName].ToString()); } /// /// Retrieve the Int32 value of the "ColName" attribute from an attribute. /// /// The Int32 value of the column name. /// The data row /// The enumeration value public static Boolean GetBoolean(System.Data.DataRow dataRow, Enum enumValue) { string columnName = GetName(enumValue); string Data = dataRow[columnName].ToString(); return (Data == "true" || Data == "1"); } } }