internal static Dictionary <string, string> GetSchemas(string conn, string type) { ConnBean connBean = ConnBean.Create(conn); int hash = connBean.GetHashCode(); Dictionary <int, Dictionary <string, string> > schemaDic = null; switch (type) { case "U": schemaDic = _TableCache; break; case "V": schemaDic = _ViewCache; break; case "P": schemaDic = _ProcCache; break; } if (schemaDic.ContainsKey(hash)) { return(schemaDic[hash]); } Dictionary <string, string> tables = null; #region 查看有没有表架构缓存 if (!string.IsNullOrEmpty(AppConfig.DB.SchemaMapPath)) { string fullPath = AppConfig.RunPath + AppConfig.DB.SchemaMapPath + hash + ".ts"; if (System.IO.File.Exists(fullPath)) { tables = JsonHelper.Split(IOHelper.ReadAllText(fullPath)); } } #endregion if (tables == null) { lock (ot) { if (!schemaDic.ContainsKey(hash)) { #region 从数据库读 using (DalBase helper = DalCreate.CreateDal(connBean.ConnName)) { helper.IsRecordDebugInfo = false; switch (type) { case "U": tables = helper.GetTables(); break; case "V": tables = helper.GetViews(); break; case "P": tables = helper.GetProcs(); break; } } } else { return(schemaDic[hash]); } } #endregion } #region 写入表结构映射 if (!string.IsNullOrEmpty(AppConfig.DB.SchemaMapPath)) { string fullPath = AppConfig.RunPath + AppConfig.DB.SchemaMapPath + hash + ".ts"; IOHelper.Save(fullPath, JsonHelper.ToJson(tables), false, true); } #endregion #region 写入缓存 if (!schemaDic.ContainsKey(hash) && tables != null && tables.Count > 0)//读不到表不缓存。 { schemaDic.Add(hash, tables); } #endregion return(tables); }
internal bool OracleBulkCopyInsert() { ConnBean bean = ConnBean.Create(_Conn); if (bean == null) { string err = "MDataTableBatchAction.OracleBulkCopyInsert ConnBean can't create by " + _Conn; Log.Write(err, LogType.DataBase); Error.Throw(err); } string conn = bean.ConnString; CheckGUIDAndDateTime(DataBaseType.Oracle); Assembly ass = OracleDal.GetAssembly(); object sbc = ass.CreateInstance("Oracle.DataAccess.Client.OracleBulkCopy", false, BindingFlags.CreateInstance, null, new object[] { conn }, null, null); Type sbcType = sbc.GetType(); try { sbcType.GetProperty("BatchSize").SetValue(sbc, 100000, null); sbcType.GetProperty("BulkCopyTimeout").SetValue(sbc, AppConfig.DB.CommandTimeout, null); sbcType.GetProperty("DestinationTableName").SetValue(sbc, SqlFormat.Keyword(mdt.TableName, DataBaseType.Oracle), null); PropertyInfo cInfo = sbcType.GetProperty("ColumnMappings"); object cObj = cInfo.GetValue(sbc, null); MethodInfo addMethod = cInfo.PropertyType.GetMethods()[4]; foreach (MCellStruct column in mdt.Columns) { addMethod.Invoke(cObj, new object[] { column.ColumnName, column.ColumnName }); } sbcType.GetMethods()[4].Invoke(sbc, new object[] { mdt }); return(true); } catch (Exception err) { if (err.InnerException != null) { err = err.InnerException; } sourceTable.DynamicData = err; Log.Write(err, LogType.DataBase); return(false); } finally { sbcType.GetMethod("Dispose").Invoke(sbc, null); } //using (Oracle.DataAccess.Client.OracleBulkCopy sbc = new OracleBulkCopy(conn, OracleBulkCopyOptions.Default)) //{ // sbc.BatchSize = 100000; // sbc.DestinationTableName = mdt.TableName; // foreach (MCellStruct column in mdt.Columns) // { // sbc.ColumnMappings.Add(column.ColumnName, column.ColumnName); // } // sbc.WriteToServer(mdt); //} //return true; }
/// <summary> /// 获取数据库链接的数据库类型 /// </summary> /// <param name="conn">链接配置Key或数据库链接语句</param> /// <returns></returns> public static DalType GetDalType(string conn) { return(ConnBean.Create(conn).ConnDalType); }
/// <summary> /// 将原有的初始化改造成延时加载。 /// </summary> private void SetDelayInit(Object entityInstance, string tableName, string conn, AopOp op) { if (string.IsNullOrEmpty(conn)) { //不设置链接,则忽略(当成普通的实体类) return; } 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 = ColumnSchema.GetColumns(typeInfo); ConnBean connBean = ConnBean.Create(conn);//下面指定链接,才不会在主从备时被切换到其它库。 if (!DBTool.ExistsTable(tableName, connBean.ConnString)) { DBTool.ErrorMsg = null; if (!DBTool.CreateTable(tableName, Columns, connBean.ConnString)) { Error.Throw("SimpleOrmBase :Create Table " + tableName + " Error:" + DBTool.ErrorMsg); } } } else if (isTxtDal)//文本数据库 { if (FieldSource != FieldSource.Data) { MDataColumn c2 = ColumnSchema.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.Write(err, LogType.DataBase); } throw; } }
/// <summary> /// 获取指定数据库链接的Hash值 /// </summary> /// <param name="connNameOrString">配置名或链接字符串</param> /// <returns></returns> public static int GetHashCode(string connNameOrString) { return(ConnBean.Create(connNameOrString).GetHashCode()); }