⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 user.cs

📁 这是一个编好的网上书店系统
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;
using RobertSoft.BookStore.DBClass;

namespace RobertSoft.BookStore
{
	/// <summary>
	/// User 的摘要说明。
	/// </summary>
	public class User : DBBaseClass
	{
		
		private string strPass;
		private string strMail;
		private string strRealName;
		private int nSex;
		private string strIDName;
		private string strIDNumber;
		private string strEducation;
		private string strProvince;
		private string strAddress;
		private string strPostCode;
		private string strCallNumber;
		private string strMobile;
		private int nUserLevel;
		private string nTotalConsumption;

#region "Property of User"
		
		/// <summary>
		/// Property: password
		/// </summary>
		public string Password
		{
			get
			{
				return strPass;
			}
			set
			{
				strPass = value;
			}
		}

		public string RealName
		{
			get
			{
				return strRealName;
			}
			set
			{
				strRealName = value;
			}
		}
		public int Sex
		{
			get
			{
				return nSex;
			}
			set
			{
				nSex = value;
			}
		}
		public string IDName
		{
			get
			{
				return strIDName;
			}
			set
			{
				strIDName = value;
			}
		}
		public string IDNumber
		{
			get
			{
				return strIDNumber;
			}
			set
			{
				strIDNumber = value;
			}
		}
		public string Education
		{
			get
			{
				return strEducation;
			}
			set
			{
				strEducation = value;
			}
		}
		public string Province
		{
			get
			{
				return strProvince;
			}
			set
			{
				strProvince = value;
			}
		}
		public string Address
		{
			get
			{
				return strAddress;
			}
			set
			{
				strAddress = value;
			}
		}
		public string PostCode
		{
			get
			{
				return strPostCode;
			}
			set
			{
				strPostCode = value;
			}
		}
		public string CallNumber
		{
			get
			{
				return strCallNumber;
			}
			set
			{
				strCallNumber = value;
			}
		}
		public string Mobile
		{
			get
			{
				return strMobile;
			}
			set
			{
				strMobile = value;
			}
		}
		public int UserLevel
		{
			get
			{
				return nUserLevel;
			}
			set
			{
				nUserLevel = value;
			}
		}
		public string TotalConsumption
		{
			get
			{
				return nTotalConsumption;
			}
			set
			{
				nTotalConsumption = value;
			}
		}
		/// <summary>
		/// Property: mail
		/// </summary>
		public string Mail
		{
			get
			{
				return strMail;
			}
			set
			{
				strMail = value;
			}
		}

#endregion
#region "Functions of User"
		public User()
		{

		}
		/// <summary>
		/// Check if the name existed
		/// </summary>
		/// <param name="name">string</param>
		/// <returns></returns>
		public bool IsExist(string name)
		{
			string strSql;
			strSql = "select ID from [User] where UserName = '" + name + "'";
			try
			{
				ExecuteSQLForValue(strSql);
				return true;
			}
			catch
			{
				return false;
			}
		}

		/// <summary>
		/// User login
		/// </summary>
		/// <param name="name">string</param>
		/// <param name="password">string</param>
		/// <returns></returns>
		public static bool Login(string name, string strPassword)
		{
			string strSql;
			strSql = "select ID from dbo.[User] where UserName = '" + name +"' and PasswordStr = '" + Functions.Encrypt(strPassword,1) + "'";
			try
			{
				ExecuteSQLForValue(strSql);
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Login()
		{
			string strSql;
			string name, strPassword;
			name = this.Name;
			strPassword = this.Password;
			strSql = "select ID from dbo.[User] where UserName = '" + name +"' and PasswordStr = '" + Functions.Encrypt(this.Password,1)  + "'";
			try
			{
				ExecuteSQLForValue(strSql);
				return true;
			}
			catch
			{
				return false;
			}
		}

		/// <summary>
		/// Get user's information
		/// </summary>
		/// <returns></returns>
		public bool GetUserInfo()
		{
			string strSQL = "Select * from dbo.[User] Where UserName='"	+ this.Name + "'";
			SqlConnection myCn = new SqlConnection(strConn);
			myCn.Open();
			SqlCommand myCmd = new SqlCommand(strSQL,myCn);
			try
			{
				myCmd.ExecuteNonQuery();
				SqlDataReader reader = myCmd.ExecuteReader();
				if(reader.Read())
				{
					this.ID = reader.GetInt32(0);
					this.RealName = reader.GetString(3);
					this.Sex = reader.GetInt32(4);
					this.IDName = reader.GetString(5);
					this.IDNumber = reader.GetString(6);
					this.Education = reader.GetString(7);
					this.Province = reader.GetString(8);
					this.Address = reader.GetString(9);
					this.PostCode = reader.GetString(10);
					this.CallNumber = reader.GetString(11);
					this.Mobile = reader.GetString(12);
					this.Mail = reader.GetString(13);
					this.UserLevel = reader.GetInt32(14);
					this.TotalConsumption = reader.GetString(15);
					return true;
				}
				else
				{
					return false;
				}
			}
			catch(System.Data.SqlClient.SqlException e)
			{
				throw new Exception(e.Message);
			}
			finally
			{
				myCmd.Dispose();
				myCn.Close();
			}
		}

		public string GetPassword()
		{
			Random rnd = new Random();
			StringBuilder sb = new StringBuilder();
			int i;
			for(i=0;i<32;i++)
			{
				sb.Append(rnd.Next(0,9).ToString());
			}
			string Password = sb.ToString();
			string EnPassword = Functions.Encrypt(Password,1);

			string strSQL = "Update [User] Set PasswordStr = '"
				+ EnPassword + "'"
				+ " Where UserName='" + Name + "'";

			try
			{
				ExecuteSQLCmd(strSQL);	
				return Password;
			}
			catch
			{
				throw new Exception("Get Password FAILED");
			}
		}

		/// <summary>
		/// Check user(for getting lost password)
		/// </summary>
		/// <returns>return bool value</returns>
		public bool Check()
		{
			string strSQL = "Select ID from [User] Where UserName='"
				+ Name + "'"
				+ " And EMail='" + Mail +"'";

			try
			{
				ExecuteSQLForValue(strSQL);
				return true;
			}
			catch
			{
				return false;
			}

		}

		public void AddUser()
		{
			string strSql = "INSERT INTO [User] (UserName, PasswordStr, RealName, Sex, IDName, IDNumber, Education, Province, Address, PostCode, PhoneNumber, MobilePhone, EMail, UserLevel, TotalConsumption)VALUES("
						+ "'" + this.Name + "',"
						+ "'" + Functions.Encrypt(this.Password,1) + "',"				
						+ "'" + this.RealName + "',"
						+ "" + this.nSex + ","
						+ "'" + this.IDName + "',"
						+ "'" + this.IDNumber + "',"
						+ "'" + this.Education + "',"
						+ "'" + this.Province + "',"
						+ "'" + this.Address + "',"
						+ "'" + this.PostCode + "',"
						+ "'" + this.CallNumber + "',"
						+ "'" + this.Mobile + "',"
						+ "'" + this.Mail + "',"
						+ "1,'0')";
			try
			{
				ExecuteSQLCmd(strSql);				
			}
			catch
			{
				throw new Exception("注册失败!请重试!");
			}			
		}

		public void UpdateUser()
		{
			string strSql = "UPDATE [User] SET RealName='" + this.RealName + "',"
				+ "Sex=" + this.Sex + ","
				+ "IDName='" + this.IDName + "',"
				+ "IDNumber='" + this.IDNumber + "',"
				+ "Education='" + this.Education + "',"
				+ "Province='" + this.Province + "',"
				+ "Address='" + this.Address + "',"
				+ "PostCode='" + this.PostCode + "',"
				+ "PhoneNumber='" + this.CallNumber + "',"
				+ "MobilePhone='" + this.Mobile + "',"
				+ "EMail='" + this.Mail + "' WHERE UserName='" + this.Name + "'";
			try
			{
				ExecuteSQLCmd(strSql);				
			}
			catch
			{
				throw new Exception("更新失败!请重试!");
			}			
		}

		/// <summary>
		/// Change password
		/// need Name & Password
		/// </summary>
		/// <param name="newPassword">new password (string)</param>		

		public void ChangePassword(string newPassword)
		{
			string strSQL = "Update [User] Set "
				+ "PasswordStr='" + Functions.Encrypt(newPassword,1) + "'"
				+ " Where UserName='" + this.Name + "'"
				+ " And PasswordStr='" + Functions.Encrypt(this.Password,1) + "'"; 
			try
			{
				ExecuteSQLCmd(strSQL);				
			}
			catch
			{
				throw new Exception("改变密码失败!");
			}
		}

		/// <summary>
		/// Is a supervisor
		/// </summary>
		/// <returns>return bool value</returns>
		public bool IsSupervisor()
		{
			string strManager = ConfigurationSettings.AppSettings["Manager"];			
			string [] names = strManager.Split(',');
			int i;

			for(i=0;i<names.Length;i++)
			{
				if(Name == names[i])
				{
					return true;
				}
			}

			return false;			
		}

		/// <summary>
		/// Is a supervisor
		/// </summary>
		/// <param name="name">User Name</param>
		/// <returns>return bool value</returns>
		public static bool IsSupervisor(string name)
		{
			string strManager = ConfigurationSettings.AppSettings["Manager"];
			string [] names = strManager.Split(',');
			int i;

			for(i=0;i<names.Length;i++)
			{
				if(name == names[i])
				{
					return true;
				}
			}

			return false;
		}
#endregion
	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -