public static string GetValueByKey(string tableName, string columnName, object key, bool extension = false) { string value = string.Empty; var relation = TableKeys.GetColumnRelation(tableName, columnName, key); if (string.IsNullOrEmpty(relation.columnName) == false) { var cacheDict = GetValuesCache(); var cacheKey = CreateCacheKey(tableName, columnName, key, true, extension); if (cacheDict.ContainsKey(cacheKey)) { value = cacheDict[cacheKey]; } else { var table = TableManager.GetTable(relation.tableName); string valueColName = GetValueColumnName(table, relation.columnName); try { if (extension) { value = SelectValueByKey(table, relation.columnName, key, valueColName); } else { value = TableManager.SelectScalarFromTable(table, relation.columnName, key, valueColName).ToString(); } cacheDict.Add(cacheKey, value.ToString()); } catch { //TODO вывод в лог } } } return(value); }
/// <param name="tableName">Текущая таблица</param> /// <param name="colName">Название текущей колонки</param> public static object GetKeyByValue(string tableName, string colName, object value) { object result = value; var relation = TableKeys.GetColumnRelation(tableName, colName); if (string.IsNullOrWhiteSpace(relation.columnName) == false) { int key; if (value is int)//если пришел уже ключ { result = value; } else if (int.TryParse(value.ToString(), out key))//пробуем парсить ключ { result = key; } else { var cacheDict = GetKeysCache(); var cacheKey = CreateCacheKey(tableName, colName, value, false, false); if (cacheDict.ContainsKey(cacheKey))//если закешировано { result = cacheDict[cacheKey]; } else { var relationTable = TableManager.GetTable(relation.tableName); var valueColumnName = GetValueColumnName(relationTable, relation.columnName); object selectObj = SelectKeyByValue(relationTable, relation.columnName, value, valueColumnName); if (selectObj != DBNull.Value) { result = selectObj; cacheDict.Add(cacheKey, result); } } } } return(result); }
public static SortedDictionary <string, int> GetRelativeItemsSource(string tableName, string columnName, bool addNullItem) { var relation = TableKeys.GetColumnRelation(tableName, columnName); var result = new SortedDictionary <string, int>(); if (string.IsNullOrEmpty(relation.tableName) == false) { var table = TableManager.GetTable(relation.tableName); string valueColName = GetValueColumnName(table, relation.columnName); DataRow[] rows = table.Select(); foreach (DataRow row in rows) { var key = (int)row[relation.columnName]; var value = GetRowStringView(row, relation.columnName, valueColName); result.Add(value, key); } } if (addNullItem) { result.Add("", -1); } return(result); }