項目要求
注意事項:
請簡述“QQ用戶信息管理系統”項目需求 上網查詢有關“用例圖”的概念和使用方法 QQ用戶信息管理系統的使用對象是誰?
系統中,包括哪些QQ用戶信息?
QQ用戶信息表與QQ等級表之間是什么關系?

用戶信息關系

選擇結構關鍵代碼

用戶登錄之后
1.操作數據庫類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace QQUserManageSystem
{
/// <summary>
/// 操作數據庫類
/// </summary>
class DBHandle
{
//連接字符串
private const string strConn = @"Data Source=.;Initial Catalog=QQDB;User ID=sa;Password=bdqn";
#region 檢查管理員信息
/// <summary>
/// 檢查管理員信息
/// </summary>
/// <param name="userName">管理員用戶名</param>
/// <param name="pwd">密碼</param>
/// <param name="strMsg">需返回的處理信息</param>
/// <returns>成功&失敗</returns>
public bool CheckAdminInfo(string userName, string pwd, ref string strMsg)
{
//創建數據庫連接
SqlConnection conn = new SqlConnection(strConn);
try
{
//創建Sql語句
string strSql = "select count(*) from Admin where LoginId='" + userName + "' and LoginPwd='" + pwd + "'";
conn.Open();
//創建Command命令
SqlCommand comm = new SqlCommand(strSql, conn);
int iRet = (int)comm.ExecuteScalar();
if (iRet != 1)
{
strMsg = "輸入無效!";
return false;
}
else
{
return true;
}
}
catch (Exception)
{
strMsg = "發生異常!";
return false;
}
finally
{
//關閉數據庫連接
conn.Close();
}
}
#endregion
#region 取得用戶信息列表
/// <summary>
/// 取得學生用戶列表
/// </summary>
/// <returns>DataReader</returns>
public SqlDataReader GetUserList()
{
try
{
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
StringBuilder sb = new StringBuilder();
sb.AppendLine(" SELECT");
sb.AppendLine(" a.[UserId]");
sb.AppendLine(" ,a.[UserName]");
sb.AppendLine(" ,b.[LevelName]");
sb.AppendLine(" ,a.[Email]");
sb.AppendLine(" ,a.[OnLineDay]");
sb.AppendLine(" FROM");
sb.AppendLine(" [UserInfo] a, [Level] b ");
sb.AppendLine(" WHERE");
sb.AppendLine(" a.[LevelId] = b.[LevelId]");
SqlCommand comm = new SqlCommand(sb.ToString(), conn);
return comm.ExecuteReader();
}
catch (Exception)
{
return null;
}
}
#endregion
#region 取得所有用戶的用戶編號和用戶等級
/// <summary>
/// 取得所有用戶的用戶編號和用戶等級
/// </summary>
/// <returns>DataReader</returns>
public SqlDataReader GetUserIdAndOnlineDay()
{
try
{
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
StringBuilder sb = new StringBuilder();
sb.AppendLine(" SELECT");
sb.AppendLine(" [UserId]");
sb.AppendLine(" ,[OnLineDay]");
sb.AppendLine(" FROM");
sb.AppendLine(" [UserInfo] ");
SqlCommand comm = new SqlCommand(sb.ToString(), conn);
return comm.ExecuteReader();
}
catch (Exception)
{
return null;
}
}
#endregion
#region 更新在線天數
/// <summary>
/// 更新在線天數
/// </summary>
/// <param name="userId">用戶編號</param>
/// <param name="onlineDay">在線天數</param>
/// <returns>受影響的行數&-1:異常</returns>
public int UpdateOnlineDay(int userId, double newOnlineDay)
{
try
{
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
StringBuilder sb = new StringBuilder();
sb.AppendLine("UPDATE");
sb.AppendLine(" [UserInfo]");
sb.AppendLine("SET");
sb.AppendLine(" [OnLineDay]=" + newOnlineDay);
sb.AppendLine("WHERE");
sb.AppendLine(" [UserId]=" + userId);
SqlCommand comm = new SqlCommand(sb.ToString(), conn);
return comm.ExecuteNonQuery();
}
catch (Exception)
{
return -1;
}
}
#endregion
#region 更新用戶等級
/// <summary>
/// 更新用戶等級
/// </summary>
/// <param name="userId">用戶編號</param>
/// <param name="iLevel">用戶等級</param>
/// <returns>受影響的行數&-1:異常</returns>
public int UpdateUserLevel(int userId, int iLevel)
{
try
{
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
StringBuilder sb = new StringBuilder();
sb.AppendLine(" UPDATE");
sb.AppendLine(" [UserInfo]");
sb.AppendLine(" SET");
sb.AppendLine(" [LevelId]=" + iLevel);
sb.AppendLine(" WHERE");
sb.AppendLine(" [UserId]=" + userId);
SqlCommand comm = new SqlCommand(sb.ToString(), conn);
return comm.ExecuteNonQuery();
}
catch (Exception)
{
return -1;
}
}
#endregion
#region 添加用戶
/// <summary>
/// 添加用戶
/// </summary>
/// <param name="userName">昵稱</param>
/// <param name="userPwd">密碼</param>
/// <param name="email">郵箱</param>
/// <returns>受影響行數&-1:異常</returns>
public object InsertUserInfo(string userName, string userPwd, string email)
{
SqlConnection conn = new SqlConnection(strConn);
try
{
conn.Open();
StringBuilder sb = new StringBuilder();
//插入用戶記錄
sb.AppendLine(" INSERT INTO");
sb.AppendLine(" [UserInfo]");
sb.AppendLine(" VALUES");
sb.AppendLine(" ('" + userName + "','" + userPwd + "',1,'" + email + "',0);");
//獲得插入記錄的用戶編號
sb.AppendLine(" SELECT @@Identity;");
SqlCommand comm = new SqlCommand(sb.ToString(), conn);
// return comm.ExecuteNonQuery();
return comm.ExecuteScalar();
}
catch (Exception)
{
return -1;
}
}
#endregion
#region 按用戶編號查詢用戶信息
public SqlDataReader GetUserByID(int UserID)
{
try
{
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
StringBuilder sb = new StringBuilder();
sb.AppendLine(" SELECT");
sb.AppendLine(" a.[UserId]");
sb.AppendLine(" ,a.[UserName]");
sb.AppendLine(" ,b.[LevelName]");
sb.AppendLine(" ,a.[Email]");
sb.AppendLine(" ,a.[OnLineDay]");
sb.AppendLine( "FROM");
sb.AppendLine(" [UserInfo] a, [Level] b");
sb.AppendLine(" WHERE");
sb.AppendLine(" a.[UserId] = " + UserID);
sb.AppendLine(" AND");
sb.AppendLine(" a.[LevelId] = b.[LevelId]");
SqlCommand comm = new SqlCommand(sb.ToString(), conn);
return comm.ExecuteReader();
}
catch(Exception)
{
return null;
}
}
#endregion
#region 刪除用戶
/// <summary>
/// 刪除用戶
/// </summary>
/// <param name="strUserId">用戶編號</param>
/// <returns>受影響的行數&-1:失敗</returns>
public int DeleteUserInfo(int strUserId)
{
try
{
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
StringBuilder sb = new StringBuilder();
sb.AppendLine(" DELETE FROM");
sb.AppendLine(" [UserInfo]");
sb.AppendLine( "WHERE");
sb.AppendLine(" [UserId]=" + strUserId);
SqlCommand comm = new SqlCommand(sb.ToString(), conn);
return comm.ExecuteNonQuery();
}
catch (Exception)
{
return -1;
}
}
#endregion
}
}
2.處理QQ用戶管理業務信息類
顯示(查詢)

修改更新信息
將輸入信息更新到數據庫 當輸入的在線天數非整型時給出“輸入錯誤”提示 異常發生給出相應提示 更新成功給出“修改成功”的提示

插入用戶信息
將輸入的信息增加到UserInfo表中 用戶等級默認為1,在線天數默認為1 操作成功給出“插入成功”提示 異常發生給出相應提示

刪除
根據用戶編號刪除對應的用戶的全部信息 刪除成功給出“刪除成功”提示 刪除失敗給出“刪除失敗”提示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace QQUserManageSystem
{
/// <summary>
/// 處理QQ用戶管理業務信息類
/// </summary>
class UserManager
{
private DBHandle _dbHandle = new DBHandle();//創建DBHandle的實例
const String ERRMSG = "數據操作失敗!";
const String EXCEPT = "出現異常。請與系統管理員聯系!";
#region 執行驗證管理員登錄並處理結果信息
/// <summary>
/// 執行驗證管理員登錄並處理結果信息
/// </summary>
public void Login()
{
int count = 0;
do
{
string strUserName = string.Empty;//初始化管理員登錄名
string strPwd = string.Empty;//初始化管理員密碼
count++;
Console.WriteLine("請輸入用戶名:");
strUserName = Console.ReadLine();
Console.WriteLine("請輸入密碼:");
strPwd = Console.ReadLine();
//非空驗證
if (strUserName.Equals(string.Empty) || strPwd.Equals(string.Empty))
{
Console.WriteLine("輸入錯誤,請重新輸入!\n");
continue;//重新輸入用戶名和密碼
}
else
{
// 需返回的結果信息
string strMsg = string.Empty;
//數據庫驗證
bool bRet = _dbHandle.CheckAdminInfo(strUserName, strPwd, ref strMsg);
if (bRet)
{
Console.WriteLine("登錄成功!");
// 顯示菜單
ShowMenu();
break;//退出程序
}
else
{
Console.WriteLine("登錄失敗:" + strMsg + "\n");
continue;//重新輸入用戶名和密碼
}
}
} while (count < 3);
if (count == 3)
Console.WriteLine("\n連續三次登錄失敗,退出本系統!\n");
}
#endregion
#region 顯示菜單
/// <summary>
/// 顯示菜單
/// </summary>
private void ShowMenu()
{
string option = "";
do
{
Console.WriteLine("");
Console.WriteLine("=======歡迎登錄QQ用戶信息管理系統======");
Console.WriteLine("----------------請選擇菜單項----------");
Console.WriteLine("1、顯示用戶清單");
Console.WriteLine("2、更新在線天數");
Console.WriteLine("3、添加用戶新記錄");
Console.WriteLine("4、更新用戶等級");
Console.WriteLine("5、刪除用戶記錄");
Console.WriteLine("0、退出");
Console.WriteLine("=======================================");
option = Console.ReadLine();
switch (option)
{
case "1"://顯示用戶信息
ShowUserInfo();
continue;
case "2"://更新在線天數
UpdateOnLineDay();
continue;
case "3"://添加用戶
InsertUserInfo();
continue;
case "4"://更新用戶等級
UpdateUserLevel();
continue;
case "5"://刪除用戶
DeleteUserInfo();
continue;
case "0":
if (IsExit())
{
break;//退出
}
else
{
continue;
}
default:
continue;
}
break;
} while (true);
}
#endregion
#region 顯示用戶列表
/// <summary>
/// 輸出用戶列表
/// </summary>
private void ShowUserInfo()
{
SqlDataReader reader = _dbHandle.GetUserList();
if (reader == null)
{
Console.WriteLine(EXCEPT);
return;
}
DisplayUserInfo(reader);
}
/// <summary>
/// 輸出用戶信息
/// </summary>
/// <param name="reader">查詢獲得的用戶記錄</param>
private void DisplayUserInfo(SqlDataReader reader)
{
Console.WriteLine("--------------------------------------------------------------------------------");
Console.WriteLine("編號\t昵稱\t\t等級\t\t郵箱\t\t在線天數");
Console.WriteLine("--------------------------------------------------------------------------------");
while (reader.Read())
{
Console.Write(reader["UserId"] + "\t");
Console.Write(reader["UserName"] + "\t");
Console.Write(ShowDesign((String)reader["LevelName"])+ "\t\t");
Console.Write(reader["Email"] + "\t");
Console.WriteLine(reader["OnLineDay"]);
}
Console.WriteLine("--------------------------------------------------------------------------------");
}
#endregion
#region 根據等級名稱輸出相應符號
/// <summary>
/// 根據等級名稱輸出相應符號
/// </summary>
/// <param name="strLevel">等級名稱</param>
/// <returns></returns>
private string ShowDesign(string strLevel)
{
string strDesign = string.Empty;
switch (strLevel)
{
case "無等級":
strDesign = "―";
break;
case "星星":
strDesign = "☆";
break;
case "月亮":
strDesign = "€";
break;
case "太陽":
strDesign = "◎";
break;
default:
break;
}
return strDesign;
}
#endregion
#region 更新用戶在線天數
/// <summary>
/// 更新用戶在線天數
/// </summary>
private void UpdateOnLineDay()
{
try
{
Console.WriteLine("請輸入用戶編號:");
string strUserId = Console.ReadLine();
int iUserId = Convert.ToInt32(strUserId);
Console.WriteLine("請輸入新的在線天數");
string strNewOnlineDay = Console.ReadLine();
double iNewOnlineDay = Convert.ToDouble(strNewOnlineDay);
int iRet = _dbHandle.UpdateOnlineDay(iUserId, iNewOnlineDay);
if (iRet == -1)
Console.WriteLine(ERRMSG);
else if (iRet == 0)
{
Console.WriteLine("用戶記錄不存在");
}
else
{
Console.WriteLine("修改成功!");
}
}
catch (Exception)
{
Console.WriteLine(EXCEPT);
}
}
#endregion
#region 添加一條用戶記錄
/// <summary>
/// 輸出添加用戶的結果
/// </summary>
private void InsertUserInfo()
{
Console.WriteLine("請輸入用戶昵稱:");
string strUserName = Console.ReadLine();
Console.WriteLine("請輸入用戶密碼:");
string strUserPwd = Console.ReadLine();
Console.WriteLine("請輸入用戶郵箱地址:");
string strUserEmail = Console.ReadLine();
int iRet = Convert.ToInt32(_dbHandle.InsertUserInfo(strUserName, strUserPwd, strUserEmail));
if (iRet == -1)
{
Console.WriteLine(EXCEPT);
}
else if (iRet == 0)
{
Console.WriteLine("用戶記錄不存在");
}
else
{
Console.WriteLine("插入成功!用戶編號是:" + iRet);
}
}
#endregion
#region 根據在線天數判定用戶等級
/// <summary>
/// 根據在線天數判定用戶等級
/// </summary>
/// <param name="iOnlineDay">在線天數</param>
/// <returns>/計算后的用戶等級</returns>
private int JudgeLevelByOnLineDay(double iOnlineDay)
{
const int LEVEL1 = 5;
const int LEVEL2 = 32;
const int LEVEL3 = 320;
int iNewLevel = 0;//計算后的等級
if (iOnlineDay >= LEVEL1 && iOnlineDay < LEVEL2)//5<=在線天數<32更新為星星
{
iNewLevel = 2;
}
else if (iOnlineDay >= LEVEL2 && iOnlineDay < LEVEL3)//32<=在線天數<320更新為月亮
{
iNewLevel = 3;
}
else if (iOnlineDay >= LEVEL3)//在線天數>=320更新為太陽
{
iNewLevel = 4;
}
else
{
iNewLevel = 1;
}
return iNewLevel;
}
#endregion
#region 更新用戶等級
/// <summary>
/// 更新用戶等級
/// </summary>
private void UpdateUserLevel()
{
//取得所有用戶的用戶編號和在線天數
SqlDataReader reader = _dbHandle.GetUserIdAndOnlineDay();
if (reader == null)
{
Console.WriteLine(EXCEPT);
return;
}
// Console.WriteLine("----------------------開始更新--------------------------------");
int iUserId = 0; //用戶編號
double iLineDay = 0; //用戶在線天數
int iLevelId = 0; //用戶等級
int count = 0; //更新記錄數
//循環取得每行的用戶編號和用戶等級
while (reader.Read())
{
iUserId = Convert.ToInt32(reader["UserId"]);//用戶編號的類型轉換
iLineDay = Convert.ToDouble(reader["OnLineDay"]);//用戶在線天數的類型轉換
iLevelId = JudgeLevelByOnLineDay(iLineDay);//根據在線天數判定用戶等級
_dbHandle.UpdateUserLevel(iUserId, iLevelId);
count++;
}
Console.WriteLine("本次共更新用戶記錄數:{0}", count);
Console.WriteLine("更新成功!");
// Console.WriteLine("----------------------更新結束---------------------------------");
}
#endregion
#region 刪除指定的用戶記錄
/// <summary>
/// 刪除指定的用戶記錄
/// </summary>
public void DeleteUserInfo()
{
try
{
//接收要刪除的用戶編號
Console.WriteLine("請輸入刪除的用戶編號:");
string strUserId = Console.ReadLine();
int iUserId = Convert.ToInt32(strUserId);
//按用戶編號查詢要刪除的用戶記錄
Console.WriteLine("將要刪除的用戶信息是:");
SqlDataReader reader = _dbHandle.GetUserByID(iUserId);
if (reader == null)
{
Console.WriteLine(EXCEPT);
return;
}
//確認是否要刪除用戶記錄
DisplayUserInfo(reader);
Console.WriteLine("要刪除該用戶記錄嗎?(Y/N)");
if (Console.ReadLine().Trim().ToUpper() != "Y")
{
Console.WriteLine("退出刪除操作!");
return;
}
//執行刪除操作
int iRet = _dbHandle.DeleteUserInfo(iUserId);
if (iRet == -1)
{
Console.WriteLine(ERRMSG);
}
else
{
Console.WriteLine("刪除成功!");
}
}
catch (Exception ex)
{
Console.WriteLine("刪除失敗:" + ex.Message);
}
}
#endregion
#region 是否退出
/// <summary>
/// 是否退出
/// </summary>
/// <returns>true:是;false:否</returns>
private bool IsExit()
{
Console.WriteLine("是否退出?(Y/N)");
string strRet = Console.ReadLine();
strRet = strRet.Trim().ToUpper();
if (strRet.Equals ("Y"))
{
return true;
}
else
{
return false;
}
}
#endregion
}
}
3.管理員登錄
管理員登錄后只有選擇“退出”后窗口關閉 對於管理員的非法操作要給出友好提示 對於數據庫的操作要有異常處理功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QQUserManageSystem
{
class Program
{
static void Main(string[] args)
{
//管理員登錄
UserManager manger = new UserManager();
manger.Login();
}
}
}
QQ用戶信息管理系統所涉及的技能點
掌握C#語法 掌握類、對象和方法的綜合運用
掌握數據庫、表的創建 掌握使用T-SQL語句操作和查詢數據
掌握使用ADO.NET操作數據庫
能夠使用異常處理增加代碼的安全性
