企业库综合应用的一个小sample

 /// <summary>
        /// 演示数据访问、Cache、日志记录(参数化Sql语句)
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string DB_Cache_Exception(int param)
        {
            try
            {
                string cacheName = "CahceName_" + param.ToString();
                object o = CacheMan.GetData(CacheMan.HighCache, cacheName);
                if (o == null)
                {
                    Database db = DB.Reader; // 只读帐户
                    DbCommand cmd = db.GetSqlStringCommand("SELECT COUNT(*) FROM TableName WHERE Param = @Param");
                    db.AddInParameter(cmd, "@Param", DbType.Int32, param);

                    o = db.ExecuteScalar(cmd);
                    if (o != null)
                        CacheMan.GenCache(CacheMan.HighCache, 720, cacheName, CacheMan.CachePriority.High, o);
                    else
                        return "";
                }
                return o.ToString();
            }
            catch (Exception ex)
            {
                HandleException.LogException(ex);
                return "";
            }           
        }

        public static int WriteByParam(string a, string b)
        {
            Database db = DB.Writer;
            // 可读写帐户
            DbCommand cmd = db.GetSqlStringCommand("INSERT INTO TableName(a, b) VALUE(@a, @b)");
            db.AddInParameter(cmd, "@a", DbType.String, a);
            db.AddInParameter(cmd, "@b", DbType.String, b);

            return db.ExecuteNonQuery(cmd);
        }

你可能感兴趣的:(sample)