日期:2014-05-17 浏览次数:20961 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace QQManger { class AdminTools { private SqlDataReader GetUserInfo() //获取用户数据 { try { StringBuilder sb = new StringBuilder(); sb.AppendLine("select UI.[UserId],UI.[UserName],L.[LevelName],UI.[Email],UI.[OnLineDay]"); sb.AppendLine("from [UserInfo] AS UI,[Level] AS L"); sb.AppendLine("where UI.[LevelId] = L.[LevelId]"); SqlCommand command = new SqlCommand(sb.ToString(), DBHelper.conn); DBHelper.conn.Open(); return command.ExecuteReader(); } catch (Exception ex) { Console.WriteLine(ex.Message); return null; } } public string CheckLevel(int OnLineDay) //等级计算的方法 { int level=0; string sql = string.Format("update UserInfo set LevelId ='{0}'",level); if (OnLineDay >= 150) { level = 4; } else if (OnLineDay >= 100 && OnLineDay < 150) { level = 3; } else if (OnLineDay >= 50 && OnLineDay < 100) { level = 2; } else { level = 1; } return sql; } public void UpdateUserLevel() //更新等级 { int count = 0; SqlDataReader reader = GetUserInfo(); try { while (reader.Read()) { int OnLineDay = Convert.ToInt32(reader["OnLineDay"]); string sql = CheckLevel(OnLineDay); SqlCommand command = new SqlCommand(sql, DBHelper.conn); int index = command.ExecuteNonQuery(); if (index == 1) { count++; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { DBHelper.conn.Close(); } Console.WriteLine("本次一共更新"+count+"条记录"); } } }