示例#1
0
 public static bool Remove(string name, string type, string conn)
 {
     if (!string.IsNullOrEmpty(name) && DBSchema.DBScheams.Count > 0)
     {
         conn = GetConn(name, out name, conn);
         int tableHash = TableInfo.GetHashCode(name);
         if (!string.IsNullOrEmpty(conn))
         {
             int dbHash = ConnBean.GetHashCode(conn);
             if (DBSchema.DBScheams.ContainsKey(dbHash))
             {
                 return(DBSchema.DBScheams[dbHash].Remove(tableHash, type));
             }
         }
         else
         {
             foreach (KeyValuePair <int, DBInfo> item in DBSchema.DBScheams)
             {
                 if (item.Value.Remove(tableHash, type))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
示例#2
0
        /// <summary>
        /// 获取(并缓存)数据库的“表、视图、存储过程”名称列表。
        /// </summary>
        public static DBInfo GetSchema(string conn)
        {
            ConnBean cb = ConnBean.Create(conn);

            if (cb != null)
            {
                int hash = cb.GetHashCode();
                if (!_DBScheams.ContainsKey(hash))
                {
                    lock (o)
                    {
                        if (!_DBScheams.ContainsKey(hash))
                        {
                            DBInfo dbSchema = GetSchemaDic(cb.ConnName);
                            if (dbSchema != null && (dbSchema.Tables.Count > 0 || dbSchema.Views.Count > 0 || dbSchema.Procs.Count > 0))
                            {
                                _DBScheams.Add(hash, dbSchema);
                            }
                            return(dbSchema);
                        }
                    }
                }
                if (_DBScheams.ContainsKey(hash))
                {
                    return(_DBScheams[hash]);
                }
            }
            return(null);
        }
示例#3
0
文件: CrossDB.cs 项目: jiszen/cyqdata
 public static bool Remove(string name, string type, string conn)
 {
     if (!string.IsNullOrEmpty(name) && DBSchema.DBScheams.Count > 0)
     {
         string newName = name;
         string newConn = GetConn(newName, out newName, conn);                                              //可能移到别的库去?
         if (string.IsNullOrEmpty(conn) || (name.Contains("..") && newConn.StartsWith(name.Split('.')[0]))) //已指定链接,则不切换链接
         {
             conn = newConn;
         }
         string tableHash = TableInfo.GetHashKey(newName);
         if (!string.IsNullOrEmpty(conn))
         {
             string dbHash = ConnBean.GetHashKey(conn);
             if (DBSchema.DBScheams.ContainsKey(dbHash))
             {
                 return(DBSchema.DBScheams[dbHash].Remove(tableHash, type));
             }
         }
         else
         {
             foreach (KeyValuePair <string, DBInfo> item in DBSchema.DBScheams)
             {
                 if (item.Value.Remove(tableHash, type))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
示例#4
0
        /// <summary>
        /// 用于标识(以用户为单位)的 全局事务 的唯一标识
        /// </summary>
        public static string GetTransationKey(string conn)
        {
            string key = Thread.CurrentThread.ManagedThreadId.ToString();

            if (HttpContext.Current != null)
            {
                string id = string.Empty;
                if (HttpContext.Current.Session != null)
                {
                    id = HttpContext.Current.Session.SessionID;
                }
                if (string.IsNullOrEmpty(id) && HttpContext.Current.Handler != null) // 增加判断。
                {
                    if (HttpContext.Current.Request["Token"] != null)                //避开异常:请求在此上下文中不可用(Global.asax.cs:Application_Start 方法)
                    {
                        id = HttpContext.Current.Request["Token"];
                    }
                    else if (HttpContext.Current.Request.Headers["Token"] != null)
                    {
                        id = HttpContext.Current.Request.Headers["Token"];
                    }
                }
                key = id + key;
            }
            string hash = ConnBean.GetHashKey(conn);

            return("Transation_" + key + hash);
        }
示例#5
0
 public static bool Remove(string name, string type, string conn)
 {
     if (!string.IsNullOrEmpty(name) && DBSchema.DBScheams.Count > 0)
     {
         int tableHash = TableSchema.GetTableHash(name);
         Dictionary <string, string> dic = TableSchema.GetSchemas(conn, type);
         if (dic != null && dic.ContainsKey(name))
         {
             dic.Remove(name);
         }
         if (!string.IsNullOrEmpty(conn))
         {
             int dbHash = ConnBean.GetHashCode(conn);
             if (DBSchema.DBScheams.ContainsKey(dbHash))
             {
                 return(DBSchema.DBScheams[dbHash].Remove(tableHash, type));
             }
         }
         else
         {
             foreach (KeyValuePair <int, DBInfo> item in DBSchema.DBScheams)
             {
                 if (item.Value.Remove(tableHash, type))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
示例#6
0
 /// <summary>
 /// 获得数据库表相关信息
 /// </summary>
 /// <param name="conn">指定时优先寻找。</param>
 /// <param name="name">表名</param>
 /// <returns></returns>
 public static TableInfo GetTableInfoByName(string name, string conn)
 {
     if (!string.IsNullOrEmpty(name) && DBSchema.DBScheams.Count > 0)
     {
         int tableHash = TableSchema.GetTableHash(name);
         if (!string.IsNullOrEmpty(conn))
         {
             int dbHash = ConnBean.GetHashCode(conn);
             if (DBSchema.DBScheams.ContainsKey(dbHash))
             {
                 TableInfo info = DBSchema.DBScheams[dbHash].GetTableInfo(tableHash);
                 if (info != null)
                 {
                     return(info);
                 }
             }
         }
         foreach (KeyValuePair <int, DBInfo> item in DBSchema.DBScheams)
         {
             TableInfo info = item.Value.GetTableInfo(tableHash);
             if (info != null)
             {
                 return(info);
             }
         }
     }
     return(null);
 }
示例#7
0
 /// <summary>
 /// 是否存在指定的表名、视图名、存储过程名
 /// </summary>
 /// <param name="name"></param>
 /// <param name="conn"></param>
 /// <returns></returns>
 public static bool Exists(string name, string type, string conn)
 {
     if (!string.IsNullOrEmpty(name) && DBSchema.DBScheams.Count > 0)
     {
         int tableHash = TableSchema.GetTableHash(name);
         if (!string.IsNullOrEmpty(conn))
         {
             int dbHash = ConnBean.GetHashCode(conn);
             if (DBSchema.DBScheams.ContainsKey(dbHash))
             {
                 TableInfo info = DBSchema.DBScheams[dbHash].GetTableInfo(tableHash, type);
                 if (info != null)
                 {
                     return(true);
                 }
             }
         }
         else
         {
             foreach (KeyValuePair <int, DBInfo> item in DBSchema.DBScheams)
             {
                 TableInfo info = item.Value.GetTableInfo(tableHash, type);
                 if (info != null)
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
示例#8
0
        /// <summary>
        /// 用于标识(以用户为单位)的 全局事务 的唯一标识
        /// </summary>
        public static string GetTransationKey(string conn)
        {
            string key = Thread.CurrentThread.ManagedThreadId.ToString();

            if (HttpContext.Current != null)
            {
                string id = string.Empty;
                if (HttpContext.Current.Session != null)
                {
                    id = HttpContext.Current.Session.SessionID;
                }
                else if (HttpContext.Current.Request["Token"] != null)
                {
                    id = HttpContext.Current.Request["Token"];
                }
                else if (HttpContext.Current.Request.Headers["Token"] != null)
                {
                    id = HttpContext.Current.Request.Headers["Token"];
                }
                key = id + key;
            }
            int hash = ConnBean.GetHashCode(conn);

            return("Transation_" + key + hash);
        }
示例#9
0
 internal static string GetBaseKey(string tableName, string conn)
 {
     if (string.IsNullOrEmpty(conn))
     {
         conn = CrossDB.GetConn(tableName, out tableName, conn);
     }
     return("AutoCache:" + ConnBean.GetHashKey(conn) + "." + tableName);
 }
示例#10
0
 static void ReInitConfig(string filePath)
 {
     appSettingJson = JsonHelper.ReadJson(filePath);
     _AppSettings.Clear();
     _ConnectionStrings.Clear();
     AppConfig.Clear();
     ConnBean.Clear();
     ConnObject.Clear();
     InitAddtionalConfigFiles();//加载额外的附加配置。
 }
示例#11
0
文件: CrossDB.cs 项目: jiszen/cyqdata
        /// <summary>
        /// 是否存在指定的表名、视图名、存储过程名
        /// </summary>
        /// <param name="newName"></param>
        /// <param name="conn"></param>
        /// <returns></returns>
        public static bool Exists(string name, string type, string conn)
        {
            string newName = name;
            string newConn = GetConn(newName, out newName, conn);

            if (string.IsNullOrEmpty(conn) || (name.Contains("..") && newConn.StartsWith(name.Split('.')[0])))//已指定链接,则不切换链接
            {
                conn = newConn;
            }
            //if (DBSchema.DBScheams.Count == 0 && !string.IsNullOrEmpty(conn))
            //{
            //    DBSchema.GetSchema(conn);
            //}
            if (!string.IsNullOrEmpty(newName))// && DBSchema.DBScheams.Count > 0
            {
                string tableHash = TableInfo.GetHashKey(newName);
                if (!string.IsNullOrEmpty(conn))
                {
                    string dbHash = ConnBean.GetHashKey(conn);
                    if (DBSchema.DBScheams.ContainsKey(dbHash))
                    {
                        TableInfo info = DBSchema.DBScheams[dbHash].GetTableInfo(tableHash, type);
                        if (info != null)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        DBInfo dbInfo = DBSchema.GetSchema(conn, false);
                        if (dbInfo != null)
                        {
                            TableInfo info = dbInfo.GetTableInfo(tableHash, type);
                            if (info != null)
                            {
                                return(true);
                            }
                        }
                    }
                }
                else
                {
                    foreach (KeyValuePair <string, DBInfo> item in DBSchema.DBScheams)
                    {
                        TableInfo info = item.Value.GetTableInfo(tableHash, type);
                        if (info != null)
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
示例#12
0
        /// <summary>
        /// 获取指定数据库链接的Hash值
        /// </summary>
        /// <param name="connNameOrString">配置名或链接字符串</param>
        /// <returns></returns>
        public static int GetHashCode(string connNameOrString)
        {
            ConnBean connBean = ConnBean.Create(connNameOrString);

            if (connBean == null)
            {
                string err = "DBInfo.GetHashCode ConnBean can't create by " + connNameOrString;
                Log.Write(err, LogType.DataBase);
                Error.Throw(err);
            }
            return(connBean.GetHashCode());
        }
示例#13
0
文件: CrossDB.cs 项目: jiszen/cyqdata
 /// <summary>
 /// 获得数据库表相关信息
 /// </summary>
 /// <param name="conn">指定时优先寻找。</param>
 /// <param name="name">表名</param>
 /// <returns></returns>
 public static TableInfo GetTableInfoByName(string name, string conn)
 {
     if (!string.IsNullOrEmpty(name))
     {
         name = SqlFormat.NotKeyword(name);
         string tableHash = TableInfo.GetHashKey(name);
         if (!string.IsNullOrEmpty(conn))
         {
             string dbHash = ConnBean.GetHashKey(conn);
             if (DBSchema.DBScheams.Count > 0 && DBSchema.DBScheams.ContainsKey(dbHash))
             {
                 TableInfo info = DBSchema.DBScheams[dbHash].GetTableInfo(tableHash);
                 if (info != null)
                 {
                     return(info);
                 }
             }
             else
             {
                 DBInfo dbInfo = DBSchema.GetSchema(conn, false);
                 if (dbInfo != null)
                 {
                     TableInfo info = dbInfo.GetTableInfo(tableHash);
                     if (info != null)
                     {
                         return(info);
                     }
                 }
             }
         }
         else
         {
             DBInfo dbInfo = DBSchema.GetSchema(AppConfig.DB.DefaultConn, false);//优先取默认链接
             if (dbInfo != null)
             {
                 TableInfo info = dbInfo.GetTableInfo(tableHash);
                 if (info != null)
                 {
                     return(info);
                 }
             }
         }
         foreach (KeyValuePair <string, DBInfo> item in DBSchema.DBScheams)
         {
             TableInfo info = item.Value.GetTableInfo(tableHash);
             if (info != null)
             {
                 return(info);
             }
         }
     }
     return(null);
 }
示例#14
0
        /// <summary>
        /// 获取数据库链接的数据库类型
        /// </summary>
        /// <param name="conn">链接配置Key或数据库链接语句</param>
        /// <returns></returns>
        public static DataBaseType GetDataBaseType(string conn)
        {
            ConnBean connBean = ConnBean.Create(conn);

            if (connBean == null)
            {
                string err = "DBTool.GetDataBaseType ConnBean can't create by " + conn;
                Log.Write(err, LogType.DataBase);
                Error.Throw(err);
            }
            return(connBean.ConnDataBaseType);
        }
示例#15
0
 public static bool Add(string name, string type, string conn)
 {
     if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(type) && DBSchema.DBScheams.Count > 0)
     {
         int tableHash = TableInfo.GetHashCode(name);
         int dbHash    = ConnBean.GetHashCode(conn);
         if (DBSchema.DBScheams.ContainsKey(dbHash))
         {
             return(DBSchema.DBScheams[dbHash].Add(tableHash, type, name));
         }
     }
     return(false);
 }
        internal bool OracleBulkCopyInsert()
        {
            CheckGUIDAndDateTime(DataBaseType.Oracle);
            string   conn    = ConnBean.Create(_Conn).ConnString;
            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;
        }
示例#17
0
        private void sendDataAlarm(Object ob)
        {
            if (ob.GetType() == typeof(ConnBean))
            {
                ConnBean bean = ob as ConnBean;
                if (bean != null && bean.Ip2 != null)
                {
                    Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    if (bean.Ip2 == null || client == null)
                    {
                        LogUtil.instance.Log("有值为空" + (bean.Ip2 == null) + "-" + (client == null));
                        return;
                    }
                    alarmClients.TryGetValue(bean.Ip2, out client);//从Dictionary中读取报警灯Client

                    if (client != null)
                    {
                        //Encoding.ASCII.GetBytes();
                        //byte[] byteMsgOn = {  0xFE, 05, 00, 00, 0xFF, 00, 0x98, 53 };
                        //byte[] byteMsgOff = { 0xFE, 05 ,00, 00, 00 ,  00, 0xD9 ,0xC5 };
                        byte[] byteMsgOn  = CommandUtil.WriteDO(Convert.ToInt16(bean.Addr), bean.Channel - 1, true);//第几个灯,下标从0开始
                        byte[] byteMsgOff = CommandUtil.WriteDO(Convert.ToInt16(bean.Addr), bean.Channel - 1, false);

                        LogUtil.instance.Log("开始发送开灯指令:" + byteMsgOn.ToArray());
                        foreach (byte b in byteMsgOn)
                        {
                            Console.Write(b + " ");
                        }
                        client.Send(byteMsgOn); //254 5 0 1 255 0 201 245

                        Thread.Sleep(5000);     //异常   毫秒数

                        LogUtil.instance.Log("开始发送关灯指令:" + byteMsgOff.ToArray());
                        foreach (byte b in byteMsgOff)
                        {
                            Console.Write(b + " ");
                        }
                        client.Send(byteMsgOff);//254 5 0 1 0 0 136 5
                    }
                    else
                    {
                        LogUtil.instance.Log(bean.Ip + "  对应的报警IP: " + bean.Ip2 + " 未在线");
                    }
                }
                else
                {
                    LogUtil.instance.Log("电子称 " + bean.Ip + " 对应的报警器 " + bean.Ip2 + "不存在");
                }
            }
        }
示例#18
0
        public ResultBean insertWeightDataToWorkBean(DZCBean bean)//电子秤读到重量
        {
            ResultBean result  = new ResultBean();
            bool       success = false;
            string     msg     = "";

            ConnBean conn      = new ConnBean();
            bool     connExist = connMap.TryGetValue(bean.Ip, out conn);//通过关联表中找到读卡器IP


            if (conn != null)
            {
                if (conn.Ip3 != null)                              //读卡器IP
                {
                    WorkBean work = tryGetWorkBeanValue(conn.Ip3); //通过读卡器IP得到已经保存的刷卡信息
                    if (work != null)
                    {
                        work = insertWeightToMap(work, bean);

                        saveWork(work);//保存信息

                        success       = true;
                        msg           = "数据保存成功";
                        result.IsLow  = (bean.Weight - conn.Weight) < 0.00000001;//低重
                        result.Value1 = bean.Weight + "";
                        result.Value2 = conn.Weight + "";
                        result.Msg2   = "低重,发送报警。报警重量:" + conn.Weight + ",当前重量:" + bean.Weight;//在IsLow为true时才正确
                        result.Bean   = conn;
                    }
                    else
                    {
                        msg = "电子称 " + conn.Ip + " 未有用户刷卡,重量数据不保存";
                    }
                }
                else
                {
                    msg = "电子称 " + conn.Ip + " 关联的读卡器 " + conn.Ip3 + " 不存在";
                }
            }
            else
            {
                msg = "关联表中不存在电子秤IP:" + bean.Ip + "的报警器及读卡器关联信息";
            }
            result.Success = success;
            result.Msg     = msg;

            return(result);
        }
示例#19
0
 /// <summary>
 /// 是否存在指定的表名、视图名、存储过程名
 /// </summary>
 /// <param name="name"></param>
 /// <param name="conn"></param>
 /// <returns></returns>
 public static bool Exists(string name, string type, string conn)
 {
     conn = GetConn(name, out name, conn);
     if (DBSchema.DBScheams.Count == 0 && !string.IsNullOrEmpty(conn))
     {
         DBSchema.GetSchema(conn);
     }
     if (!string.IsNullOrEmpty(name) && DBSchema.DBScheams.Count > 0)
     {
         string tableHash = TableInfo.GetHashKey(name);
         if (!string.IsNullOrEmpty(conn))
         {
             string dbHash = ConnBean.GetHashKey(conn);
             if (DBSchema.DBScheams.ContainsKey(dbHash))
             {
                 TableInfo info = DBSchema.DBScheams[dbHash].GetTableInfo(tableHash, type);
                 if (info != null)
                 {
                     return(true);
                 }
             }
             else
             {
                 DBInfo dbInfo = DBSchema.GetSchema(conn);
                 if (dbInfo != null)
                 {
                     TableInfo info = dbInfo.GetTableInfo(tableHash, type);
                     if (info != null)
                     {
                         return(true);
                     }
                 }
             }
         }
         else
         {
             foreach (KeyValuePair <string, DBInfo> item in DBSchema.DBScheams)
             {
                 TableInfo info = item.Value.GetTableInfo(tableHash, type);
                 if (info != null)
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
示例#20
0
        /// <summary>
        /// 获取(并缓存)数据库的“表、视图、存储过程”名称列表。
        /// </summary>
        public static DBInfo GetSchema(string conn, bool isOuterLocking)
        {
            ConnBean cb = ConnBean.Create(conn);

            if (cb != null)
            {
                string hash = cb.GetHashKey();
                if (!_DBScheams.ContainsKey(hash))
                {
                    if (!isOuterLocking && _DBScheamsTemp.ContainsKey(hash))
                    {
                        return(_DBScheamsTemp[hash]);
                    }

                    lock (o)
                    {
                        if (!_DBScheams.ContainsKey(hash))
                        {
                            DBInfo dbSchema = GetSchemaDic(cb.ConnName);
                            if (dbSchema != null && (dbSchema.Tables.Count > 0 || dbSchema.Views.Count > 0 || dbSchema.Procs.Count > 0))
                            {
                                if (isOuterLocking)//外部有lock,外部不会产生foreach代码时。
                                {
                                    if (!_DBScheams.ContainsKey(hash))
                                    {
                                        _DBScheams.Add(hash, dbSchema);
                                    }
                                }
                                else
                                {
                                    if (!_DBScheamsTemp.ContainsKey(hash))
                                    {
                                        _DBScheamsTemp.Add(hash, dbSchema);
                                    }
                                }
                            }
                            return(dbSchema);
                        }
                    }
                }
                if (_DBScheams.ContainsKey(hash))
                {
                    return(_DBScheams[hash]);
                }
            }
            return(null);
        }
示例#21
0
 public static bool Add(string name, string type, string conn)
 {
     if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(type) && DBSchema.DBScheams.Count > 0)
     {
         int tableHash = TableSchema.GetTableHash(name);
         Dictionary <string, string> dic = TableSchema.GetSchemas(conn, type);
         if (dic != null && !dic.ContainsKey(name))
         {
             dic.Add(name, name);
         }
         int dbHash = ConnBean.GetHashCode(conn);
         if (DBSchema.DBScheams.ContainsKey(dbHash))
         {
             return(DBSchema.DBScheams[dbHash].Add(tableHash, type, name));
         }
     }
     return(false);
 }
示例#22
0
文件: CrossDB.cs 项目: jiszen/cyqdata
 public static bool Add(string name, string type, string conn)
 {
     if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(type) && DBSchema.DBScheams.Count > 0)
     {
         string newName = name;
         string newConn = GetConn(newName, out newName, conn);
         if (string.IsNullOrEmpty(conn) || (name.Contains("..") && newConn.StartsWith(name.Split('.')[0])))//已指定链接,则不切换链接
         {
             conn = newConn;
         }
         string tableHash = TableInfo.GetHashKey(newName);
         string dbHash    = ConnBean.GetHashKey(conn);
         if (DBSchema.DBScheams.ContainsKey(dbHash))
         {
             return(DBSchema.DBScheams[dbHash].Add(tableHash, type, newName));
         }
     }
     return(false);
 }
示例#23
0
        /// <summary>
        /// 获取数据库的“表、视图、存储过程”名称列表。
        /// </summary>
        public static DBInfo GetSchema(string conn)
        {
            ConnBean cb   = ConnBean.Create(conn);
            int      hash = cb.GetHashCode();

            if (!_DBScheams.ContainsKey(hash))
            {
                lock (o)
                {
                    if (!_DBScheams.ContainsKey(hash))
                    {
                        DBInfo dbSchema = GetSchemaDic(cb.ConnString);
                        if (dbSchema != null)
                        {
                            _DBScheams.Add(hash, dbSchema);
                        }
                        return(dbSchema);
                    }
                }
            }
            return(null);
        }
示例#24
0
        /// <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;
            }
        }
示例#25
0
 /// <summary>
 /// 获取数据库链接的数据库类型
 /// </summary>
 /// <param name="conn">链接配置Key或数据库链接语句</param>
 /// <returns></returns>
 public static DataBaseType GetDataBaseType(string conn)
 {
     return(ConnBean.Create(conn).ConnDataBaseType);
 }
示例#26
0
        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);
        }
示例#27
0
 /// <summary>
 /// 获取指定数据库链接的Hash值
 /// </summary>
 /// <param name="connNameOrString">配置名或链接字符串</param>
 /// <returns></returns>
 public static int GetHashCode(string connNameOrString)
 {
     return(ConnBean.Create(connNameOrString).GetHashCode());
 }
示例#28
0
 internal static string GetBaseKey(string tableName, string connString)
 {
     return("AutoCache:" + ConnBean.GetHashCode(connString) + "." + tableName);
 }