/// <summary> /// 判断当前节点是否已存在相同的 /// </summary> /// <param name="entity"></param> /// <returns></returns> public int ExistNum(WeChatTokenLogEntity entity) { ///id=0,判断总数,ID>0判断除自己之外的总数 string sql = @"Select count(1) from dbo.[WeChatTokenLog] WITH(NOLOCK) "; string where = "where "; if (entity.Id == 0) { } else { } sql = sql + where; DbCommand cmd = db.GetSqlStringCommand(sql); if (entity.Id > 0) { db.AddInParameter(cmd, "@Id", DbType.Int32, entity.Id); } object identity = db.ExecuteScalar(cmd); if (identity == null || identity == DBNull.Value) { return(0); } return(Convert.ToInt32(identity)); }
public string GetAccessToken(bool cache = false) { string token = ""; string _cachekey = "GetWeChatAccessToken"; object obj = MemCache.GetCache(_cachekey); //从缓存获取 if (cache && obj != null) { WeChatTokenLogEntity log = (WeChatTokenLogEntity)obj; if (log.EndTime > DateTime.Now) { token = log.AccessToken; } } //从数据库获取 if (token == "") { string appid = WeiXinConfig.GetAppId(); WeChatTokenLogEntity log = WeChatTokenLogBLL.Instance.GetTokenByAppid(appid); if (log != null && log.Id > 0 && log.EndTime > DateTime.Now) { token = log.AccessToken; TimeSpan ts1 = log.EndTime - DateTime.Now; int tsSen = ts1.Seconds; if (cache) { MemCache.AddCache(_cachekey, log, tsSen); } } if (token == "") { string result = WebServiceClient.QueryGetWebService(string.Format(WeiXinConfig.URL_FORMAT_TOKEN, WeiXinConfig.GetAppId(), WeiXinConfig.GetAppSecret()), "", null); JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary <string, object> jsonObj = serializer.Deserialize <dynamic>(result); if (jsonObj.ContainsKey("access_token")) { token = jsonObj["access_token"].ToString(); WeChatTokenLogEntity tokenlog = new WeChatTokenLogEntity(); tokenlog.Appid = appid; tokenlog.CreateTime = DateTime.Now; tokenlog.EndTime = DateTime.Now.AddSeconds(7000); tokenlog.AccessToken = token; WeChatTokenLogBLL.Instance.AddWeChatTokenLog(tokenlog); if (cache) { MemCache.AddCache(_cachekey, tokenlog, 7000); } } else { token = ""; } } } return(token); }
/// <summary> /// 根据主键值更新记录的全部字段(注意:该方法不会对自增字段、timestamp类型字段以及主键字段更新!如果要更新主键字段,请使用Update方法)。 /// 如果数据库有数据被更新了则返回True,否则返回False /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="weChatTokenLog">待更新的实体对象</param> public int UpdateWeChatTokenLog(WeChatTokenLogEntity entity) { string sql = @" UPDATE dbo.[WeChatTokenLog] SET [Appid]=@Appid,[AccessToken]=@AccessToken,[CreateTime]=@CreateTime,[EndTime]=@EndTime WHERE [Id]=@id"; DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@Id", DbType.Int32, entity.Id); db.AddInParameter(cmd, "@Appid", DbType.String, entity.Appid); db.AddInParameter(cmd, "@AccessToken", DbType.String, entity.AccessToken); db.AddInParameter(cmd, "@CreateTime", DbType.DateTime, entity.CreateTime); db.AddInParameter(cmd, "@EndTime", DbType.DateTime, entity.EndTime); return(db.ExecuteNonQuery(cmd)); }
/// <summary> /// 插入一条记录到表WeChatTokenLog,如果表中存在自增字段,则返回值为新记录的自增字段值,否则返回0 /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="weChatTokenLog">待插入的实体对象</param> public int AddWeChatTokenLog(WeChatTokenLogEntity entity) { string sql = @"insert into WeChatTokenLog( [Appid],[AccessToken],[CreateTime],[EndTime])VALUES ( @Appid,@AccessToken,@CreateTime,@EndTime); SELECT SCOPE_IDENTITY();" ; DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@Appid", DbType.String, entity.Appid); db.AddInParameter(cmd, "@AccessToken", DbType.String, entity.AccessToken); db.AddInParameter(cmd, "@CreateTime", DbType.DateTime, entity.CreateTime); db.AddInParameter(cmd, "@EndTime", DbType.DateTime, entity.EndTime); object identity = db.ExecuteScalar(cmd); if (identity == null || identity == DBNull.Value) { return(0); } return(Convert.ToInt32(identity)); }
/// <summary> /// 读取记录列表。 /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="columns">需要返回的列,不提供任何列名时默认将返回所有列</param> public IList <WeChatTokenLogEntity> GetWeChatTokenLogList(int pagesize, int pageindex, ref int recordCount) { string sql = @"SELECT [Id],[Appid],[AccessToken],[CreateTime],[EndTime] FROM (SELECT ROW_NUMBER() OVER (ORDER BY Id desc) AS ROWNUMBER, [Id],[Appid],[AccessToken],[CreateTime],[EndTime] from dbo.[WeChatTokenLog] WITH(NOLOCK) WHERE 1=1 ) as temp where rownumber BETWEEN ((@PageIndex - 1) * @PageSize + 1) AND @PageIndex * @PageSize" ; string sql2 = @"Select count(1) from dbo.[WeChatTokenLog] with (nolock) "; IList <WeChatTokenLogEntity> entityList = new List <WeChatTokenLogEntity>(); DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@PageIndex", DbType.Int32, pageindex); db.AddInParameter(cmd, "@PageSize", DbType.Int32, pagesize); using (IDataReader reader = db.ExecuteReader(cmd)) { while (reader.Read()) { WeChatTokenLogEntity entity = new WeChatTokenLogEntity(); entity.Id = StringUtils.GetDbInt(reader["Id"]); entity.Appid = StringUtils.GetDbString(reader["Appid"]); entity.AccessToken = StringUtils.GetDbString(reader["AccessToken"]); entity.CreateTime = StringUtils.GetDbDateTime(reader["CreateTime"]); entity.EndTime = StringUtils.GetDbDateTime(reader["EndTime"]); entityList.Add(entity); } } cmd = db.GetSqlStringCommand(sql2); using (IDataReader reader = db.ExecuteReader(cmd)) { if (reader.Read()) { recordCount = StringUtils.GetDbInt(reader[0]); } else { recordCount = 0; } } return(entityList); }
/// <summary> /// 读取记录列表。 /// </summary> /// <param name="db">数据库操作对象</param> /// <param name="columns">需要返回的列,不提供任何列名时默认将返回所有列</param> public IList <WeChatTokenLogEntity> GetWeChatTokenLogAll() { string sql = @"SELECT [Id],[Appid],[AccessToken],[CreateTime],[EndTime] from dbo.[WeChatTokenLog] WITH(NOLOCK) "; IList <WeChatTokenLogEntity> entityList = new List <WeChatTokenLogEntity>(); DbCommand cmd = db.GetSqlStringCommand(sql); using (IDataReader reader = db.ExecuteReader(cmd)) { while (reader.Read()) { WeChatTokenLogEntity entity = new WeChatTokenLogEntity(); entity.Id = StringUtils.GetDbInt(reader["Id"]); entity.Appid = StringUtils.GetDbString(reader["Appid"]); entity.AccessToken = StringUtils.GetDbString(reader["AccessToken"]); entity.CreateTime = StringUtils.GetDbDateTime(reader["CreateTime"]); entity.EndTime = StringUtils.GetDbDateTime(reader["EndTime"]); entityList.Add(entity); } } return(entityList); }
public WeChatTokenLogEntity GetTokenByAppid(string appid) { string sql = @"SELECT top 1 [Id],[Appid],[AccessToken],[CreateTime],[EndTime] FROM dbo.[WeChatTokenLog] WITH(NOLOCK) WHERE [Appid]=@Appid and EndTime>getdate()" ; DbCommand cmd = db.GetSqlStringCommand(sql); db.AddInParameter(cmd, "@Appid", DbType.String, appid); WeChatTokenLogEntity entity = new WeChatTokenLogEntity(); using (IDataReader reader = db.ExecuteReader(cmd)) { if (reader.Read()) { entity.Id = StringUtils.GetDbInt(reader["Id"]); entity.Appid = StringUtils.GetDbString(reader["Appid"]); entity.AccessToken = StringUtils.GetDbString(reader["AccessToken"]); entity.CreateTime = StringUtils.GetDbDateTime(reader["CreateTime"]); entity.EndTime = StringUtils.GetDbDateTime(reader["EndTime"]); } } return(entity); }
/// <summary> /// 更新一条WeChatTokenLog记录。 /// 该方法提供给界面等UI层调用 /// </summary> /// <param name="weChatTokenLog">待更新的实体对象</param> /// <param name="columns">要更新的列名,不提供任何列名时默认将更新主键之外的所有列</param> public int UpdateWeChatTokenLog(WeChatTokenLogEntity weChatTokenLog) { return(WeChatTokenLogDA.Instance.UpdateWeChatTokenLog(weChatTokenLog)); }
/// <summary> /// 插入一条记录到表WeChatTokenLog,如果表中存在自增字段,则返回值为新记录的自增字段值,否则返回0。 /// 该方法提供给界面等UI层调用 /// </summary> /// <param name="weChatTokenLog">要添加的WeChatTokenLog数据实体对象</param> public int AddWeChatTokenLog(WeChatTokenLogEntity weChatTokenLog) { return(WeChatTokenLogDA.Instance.AddWeChatTokenLog(weChatTokenLog)); }
/// <summary> /// 判断对象是否存在 /// </summary> /// <param name="dicEnum"></param> /// <returns></returns> public bool IsExist(WeChatTokenLogEntity weChatTokenLog) { return(WeChatTokenLogDA.Instance.ExistNum(weChatTokenLog) > 0); }