Ejemplo n.º 1
0
        /// <summary>
        /// 直接运行SQL语句
        /// </summary>
        /// <param name="ConnectionName">连接名</param>
        /// <param name="sql">sql语句</param>
        /// <param name="opType">操作类型</param>
        /// <returns></returns>
        public static object RunSQL(string connectionName, string sql, emOperationType opType)
        {
            string wrongMessage = "";
            object obj          = null;

            DBLInit _DBLInit = new DBLInit(connectionName);

            switch (opType)
            {
            case emOperationType.select:
                obj = _DBLInit.GetDataTable(sql, out wrongMessage);
                break;

            case emOperationType.delete:
            case emOperationType.insert:
            case emOperationType.update:
                obj = _DBLInit.ProcessSql(sql, opType, out wrongMessage);
                break;
            }

            MyORM.Log.WriteInfo(connectionName + "-" + opType.ToString() + "-" + sql);

            if (!string.IsNullOrEmpty(wrongMessage))
            {
                MyORM.Log.WriteError(wrongMessage);
            }

            return(obj);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="obj">对象</param>
        /// <param name="tid">主键值</param>
        /// <returns></returns>
        public T Get <T>(object obj, int tid) where T : new()
        {
            //从缓存中读取该主键值的数据是否存在()
            var    tp  = obj.GetType();
            string key = tp.ToString() + "." + tid;

            MyCache.Caching cache = new MyCache.Caching();

            T v = new T();

            if (cache.IsCache(key))
            {
                v = (T)cache.Get(key);
            }
            else
            {
                string WrongMessage = null;
                string SelectSql    = CreateSelectSQL(typeof(T), obj, "this.tid=" + tid, 0);
                var    dt           = _DBLInit.GetDataTable(SelectSql, out WrongMessage);

                if (WrongMessage == null)
                {
                    List <T> myList = new List <T>(dt.Rows.Count);
                    int      i      = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        object objClass = Activator.CreateInstance(typeof(T), null);
                        myList.Add((T)DataPacking(typeof(T), objClass, dr));
                        i++;
                    }

                    if (myList.Count == 1)
                    {
                        v = myList[0];
                        cache.Add(key, v);
                    }
                }
                else
                {
                    //ClassCommon.LogError(String.Format("{0}|{1}", SelectSql, WrongMessage));
                }
            }

            return(v);
        }