/// <summary> /// 条件比较 /// </summary> private static bool GetTFilterOk(string where, int quoteIndex, string sign, Op op, MDataColumn mdc, out TFilter tFilter) { bool result = false; tFilter = null; int index = where.ToLower().IndexOf(sign, 0, quoteIndex > 0 ? quoteIndex : where.Length); if (index > 0) { string columnAName = where.Substring(0, index).Trim(); int columnAIndex = mdc.GetIndex(columnAName); string valueB = where.Substring(index + sign.Length).Trim(' ', '\'').Replace("''", "'"); if (op == Op.In || op == Op.NotIn) { valueB = ',' + valueB.TrimStart('(', ')').Replace("'", "") + ",";//去除单引号。 } int columnBIndex = -1; if (quoteIndex == 0 && mdc.Contains(valueB)) //判断右侧的是否列名。 { columnBIndex = mdc.GetIndex(valueB); } tFilter = new TFilter(Ao.None, columnAName, columnAIndex, op, valueB, columnBIndex); if (columnBIndex == -1 && !string.IsNullOrEmpty(Convert.ToString(valueB))) //右侧是值类型,转换值的类型。 { if (columnAIndex > -1 && DataType.GetGroup(mdc[columnAIndex].SqlType) == 3) //bool型 { switch (Convert.ToString(tFilter._valueB).ToLower()) { case "true": case "1": case "on": tFilter._valueB = true; break; case "false": case "0": case "": tFilter._valueB = false; break; default: tFilter._valueB = null; break; } } else { try { tFilter._valueB = StaticTool.ChangeType(tFilter._valueB, columnAIndex > -1 ? typeof(string) : mdc[columnAIndex].ValueType); } catch { } } } result = true; } return(result); }
/// <summary> /// Returns the value of the first column of the first row /// <para>返回首行首列的值</para> /// </summary> public T ExeScalar <T>() { CheckDisposed(); AopResult aopResult = SetAopResult(AopEnum.ExeScalar); if (aopResult == AopResult.Default || aopResult == AopResult.Continue) { switch (dalHelper.dalType) { case DalType.Txt: case DalType.Xml: _aop.Para.ExeResult = _noSqlCommand.ExeScalar(); break; default: _aop.Para.ExeResult = dalHelper.ExeScalar(_procName, _isProc); break; } _aop.Para.IsSuccess = _aop.Para.ExeResult != null; } if (aopResult == AopResult.Continue || aopResult == AopResult.Break) { _aop.End(AopEnum.ExeScalar); } if (_aop.Para.ExeResult == null || _aop.Para.ExeResult == DBNull.Value) { return(default(T)); } Type t = typeof(T); object value = _aop.Para.ExeResult; switch (t.Name) { case "Int32": int intValue = 0; if (!int.TryParse(Convert.ToString(value), out intValue)) { return(default(T)); } value = intValue; break; default: try { value = StaticTool.ChangeType(value, t); } catch { } break; } return((T)value); }
/// <summary> /// 获得一个Cache对象 /// </summary> public T Get <T>(string key) { object o = Get(key); if (o != null) { Type t = typeof(T); try { return((T)StaticTool.ChangeType(o, t)); } catch (Exception err) { Log.WriteLogToTxt(err); return(default(T)); } } return(default(T)); }
/// <summary> /// 值的数据类型转换。 /// </summary> /// <param name="value">要被转换的值</param> /// <param name="convertionType">要转换成哪种类型</param> /// <param name="groupID">数据库类型的组号</param> /// <returns></returns> internal object ChangeValue(object value, Type convertionType, int groupID, out Exception ex) { ex = null; strValue = Convert.ToString(value); if (value == null) { cellValue.IsNull = true; return(value); } if (groupID > 0 && strValue == "") { cellValue.IsNull = true; return(null); } try { #region 类型转换 if (groupID == 1) { switch (strValue) { case "正无穷大": strValue = "Infinity"; break; case "负无穷大": strValue = "-Infinity"; break; } } if (value.GetType() != convertionType) { #region 折叠 switch (groupID) { case 0: if (_CellStruct.SqlType == SqlDbType.Time) //time类型的特殊处理。 { string[] items = strValue.Split(' '); if (items.Length > 1) { strValue = items[1]; } } value = strValue; break; case 1: switch (strValue.ToLower()) { case "true": value = 1; break; case "false": value = 0; break; case "infinity": value = double.PositiveInfinity; break; case "-infinity": value = double.NegativeInfinity; break; default: goto err; } break; case 2: switch (strValue.ToLower().TrimEnd(')', '(')) { case "now": case "getdate": case "current_timestamp": value = DateTime.Now; break; default: DateTime dt = DateTime.Parse(strValue); value = dt == DateTime.MinValue ? (DateTime)SqlDateTime.MinValue : dt; break; } break; case 3: switch (strValue.ToLower()) { case "yes": case "true": case "1": case "on": case "是": value = true; break; case "no": case "false": case "0": case "": case "否": default: value = false; break; } break; case 4: if (strValue == SqlValue.Guid || strValue.StartsWith("newid")) { value = Guid.NewGuid(); } else { value = new Guid(strValue); } break; default: err: if (convertionType.Name.EndsWith("[]")) { value = Convert.FromBase64String(strValue); strValue = "System.Byte[]"; } else { value = StaticTool.ChangeType(value, convertionType); } break; } #endregion } //else if (groupID == 2 && strValue.StartsWith("000")) //{ // value = SqlDateTime.MinValue; //} #endregion } catch (Exception err) { value = null; cellValue.Value = null; cellValue.IsNull = true; ex = err; string msg = string.Format("ChangeType Error:ColumnName【{0}】({1}) , Value:【{2}】\r\n", _CellStruct.ColumnName, _CellStruct.ValueType.FullName, strValue); strValue = null; if (AppConfig.Log.IsWriteLog) { Log.WriteLog(true, msg); } } return(value); }