/// <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); }
private static string GetKey(AopEnum action, AopInfo aopInfo, string baseKey) { StringBuilder sb = new StringBuilder(); sb.Append(baseKey); switch (action) { case AopEnum.ExeNonQuery: case AopEnum.Insert: case AopEnum.Update: case AopEnum.Delete: return(sb.ToString()); } #region Key1:DBType sb.Append("."); sb.Append(action); if (aopInfo.DBParameters != null && aopInfo.DBParameters.Count > 0) { foreach (DbParameter item in aopInfo.DBParameters) { sb.Append(item.ParameterName); sb.Append(item.Value); } } if (aopInfo.CustomDbPara != null) { foreach (AopCustomDbPara item in aopInfo.CustomDbPara) { sb.Append(item.ParaName); sb.Append(item.Value); } } if (aopInfo.SelectColumns != null) { foreach (object item in aopInfo.SelectColumns) { sb.Append(item); sb.Append(item); } } #endregion switch (action) { case AopEnum.ExeMDataTableList: case AopEnum.ExeMDataTable: case AopEnum.ExeScalar: sb.Append(aopInfo.IsProc); sb.Append(aopInfo.ProcName); break; case AopEnum.Exists: case AopEnum.Fill: case AopEnum.GetCount: sb.Append(aopInfo.TableName); sb.Append(aopInfo.Where); break; case AopEnum.Select: sb.Append(aopInfo.TableName); sb.Append(aopInfo.PageIndex); sb.Append(aopInfo.PageSize); sb.Append(aopInfo.Where); break; } return(StaticTool.GetHashKey(sb.ToString())); }
/// <summary> /// 将原有的初始化改造成延时加载。 /// </summary> private void SetDelayInit(Object entityInstance, string tableName, string conn, AopOp op) { conn = string.IsNullOrEmpty(conn) ? AppConfig.DB.DefaultConn : conn; entity = entityInstance; typeInfo = entity.GetType(); try { if (string.IsNullOrEmpty(tableName)) { tableName = typeInfo.Name; if (tableName.EndsWith(AppConfig.EntitySuffix)) { tableName = tableName.Substring(0, tableName.Length - AppConfig.EntitySuffix.Length); } } string key = tableName + StaticTool.GetHashKey(conn); if (!CacheManage.LocalInstance.Contains(key)) { DalType dal = DBTool.GetDalType(conn); bool isTxtDal = dal == DalType.Txt || dal == DalType.Xml; string errMsg = string.Empty; Columns = DBTool.GetColumns(tableName, conn, out errMsg);//内部链接错误时抛异常。 if (Columns == null || Columns.Count == 0) { if (errMsg != string.Empty) { Error.Throw(errMsg); } Columns = TableSchema.GetColumns(typeInfo); if (!DBTool.ExistsTable(tableName, conn)) { DBTool.ErrorMsg = null; if (!DBTool.CreateTable(tableName, Columns, conn)) { Error.Throw("SimpleOrmBase :Create Table Error:" + tableName + DBTool.ErrorMsg); } } } else if (isTxtDal)//文本数据库 { if (FieldSource != FieldSource.Data) { MDataColumn c2 = TableSchema.GetColumns(typeInfo); if (FieldSource == FieldSource.BothOfAll) { Columns.AddRange(c2); } else { Columns = c2; } } } if (Columns != null && Columns.Count > 0) { CacheManage.LocalInstance.Set(key, Columns, 1440, null); } } else { Columns = CacheManage.LocalInstance.Get(key) as MDataColumn; } _Action = new MAction(Columns.ToRow(tableName), conn); if (typeInfo.Name == "SysLogs") { _Action.SetAopState(Aop.AopOp.CloseAll); } else { _Action.SetAopState(op); } _Action.EndTransation(); } catch (Exception err) { if (typeInfo.Name != "SysLogs") { Log.WriteLogToTxt(err); } throw; } }