?? user.cs
字號:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;
namespace Hugo.BookShop
{
/// <summary>
/// User Class
/// Manage users
/// </summary>
public class User:DbBase.Base
{
private string m_Password;
private string m_Mail;
/// <summary>
/// Property:password
/// </summary>
public string Password
{
get
{
return m_Password;
}
set
{
m_Password = value;
}
}
/// <summary>
/// Property:Email
/// </summary>
public string Mail
{
get
{
return m_Mail;
}
set
{
m_Mail = value;
}
}
public User()
{
}
/// <summary>
/// Add new user
/// need Name、Password、Mail.
/// </summary>
public void Add()
{
if(IsExist())
{
throw new Exception("This name was registered!");
}
else
{
strSQL = "Insert into UserInfo (Name,Password,Mail) Values("
+ "'" + this.Name + "',"
+ "'" + Functions.Encrypt(this.Password,1) + "',"
+ "'" + this.Mail + "')";
try
{
ExecuteSql(strSQL);
}
catch
{
throw new Exception("Register FAILED!");
}
}
}
/// <summary>
/// Add new user(register).
/// </summary>
public static void Add(string name,string password,string mail)
{
if(IsExist(name))
{
throw new Exception("This name was registered!");
}
else
{
strSQL = "Insert into UserInfo (Name,Password,Mail) Values("
+ "'" + name + "',"
+ "'" + Functions.Encrypt(password,1) + "',"
+ "'" + mail + "')";
try
{
ExecuteSql(strSQL);
}
catch
{
throw new Exception("Register FAILED!");
}
}
}
/// <summary>
/// Change password
/// need Name & Password
/// </summary>
/// <param name="newPassword">new password (string)</param>
public void ChangePassword(string newPassword)
{
strSQL = "Update UserInfo Set "
+ "Password='" + Functions.Encrypt(newPassword,1) + "'"
+ " Where Name='" + this.Name + "'"
+ " And Password='" + Functions.Encrypt(this.Password,1) + "'";
try
{
ExecuteSql(strSQL);
}
catch
{
throw new Exception("Change password FAILED!");
}
}
/// <summary>
/// Change password
/// </summary>
/// <param name="name"></param>
/// <param name="oldPassword">Old password(string)</param>
/// <param name="newPassword">New password(string)</param>
public static void ChangePassword(string name,string oldPassword,string newPassword)
{
strSQL = "Update UserInfo Set "
+ "Password='" + Functions.Encrypt(newPassword,1) + "'"
+ " Where Name='" + name + "'"
+ " And Password='" + Functions.Encrypt(oldPassword,1) + "'";
try
{
ExecuteSql(strSQL);
}
catch
{
throw new Exception("Change password FAILED!");
}
}
/// <summary>
/// Check user(for getting lost password)
/// </summary>
/// <returns>return bool value</returns>
public bool Check()
{
strSQL = "Select Id from UserInfo Where Name='"
+ Name + "'"
+ " And Mail='" + Mail +"'";
try
{
ExecuteSql4Value(strSQL);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Check user(for getting lost password)
/// </summary>
/// <param name="name">Name</param>
/// <param name="mail">Email</param>
/// <returns>return bool value</returns>
public static bool Check(string name,string mail)
{
strSQL = "Select Id from UserInfo Where Name='"
+ name + "'"
+ " And Mail='" + mail +"'";
try
{
ExecuteSql4Value(strSQL);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Delete user
/// </summary>
/// <param name="Id">User ID(int)</param>
public static void Delete(int id)
{
strSQL = "Delete From UserInfo Where Id="+id;
try
{
ExecuteSql(strSQL);
}
catch
{
throw new Exception("Delete user FAILED!");
}
}
/// <summary>
/// Delete user
/// </summary>
public void Delete()
{
strSQL = "Delete From UserInfo Where Name="+Name;
try
{
ExecuteSql(strSQL);
}
catch
{
throw new Exception("Delete user FAILED!");
}
}
/// <summary>
/// Delete user
/// </summary>
/// <param name="Name">User name(string)</param>
public static void Delete(string name)
{
strSQL = "Delete From UserInfo Where Name="+name;
try
{
ExecuteSql(strSQL);
}
catch
{
throw new Exception("Delete user FAILED!");
}
}
/// <summary>
/// Delete a group user
/// </summary>
/// <param name="names">Users' names</param>
public static void DeleteGroup(string names)
{
strSQL = "Delete From UserInfo Where Name in ('" + names + "')";
try
{
ExecuteSql(strSQL);
}
catch
{
throw new Exception("Delete user FAILED!");
}
}
/// <summary>
/// Does this user exist?
/// </summary>
/// <returns>return bool value</returns>
public bool IsExist()
{
strSQL = "Select Id from UserInfo Where Name='"
+ this.Name + "'";
try
{
ExecuteSql4Value(strSQL);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Does this user exist?
/// </summary>
/// <param name="name">user name(string)</param>
/// <returns>return bool value</returns>
public static bool IsExist(string name)
{
strSQL = "Select Id from UserInfo Where Name='"
+ name + "'";
try
{
ExecuteSql4Value(strSQL);
return true;
}
catch
{
return false;
}
}
/// <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;
}
/// <summary>
/// Get password
/// </summary>
/// <returns>Password</returns>
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();//ASCIIEncoding.ASCII.GetString(random);
string EnPassword = Functions.Encrypt(Password,1);
strSQL = "Update UserInfo Set Password = '"
+ EnPassword + "'"
+ " Where Name='" + Name + "'";
try
{
ExecuteSql(strSQL);
return Password;
}
catch
{
throw new Exception("Get Password FAILED");
}
}
/// <summary>
/// Get password
/// </summary>
/// <param name="name">User name(string)</param>
/// <returns>password</returns>
public static string GetPassword(string name)
{
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();//ASCIIEncoding.ASCII.GetString(random);
string EnPassword = Functions.Encrypt(Password,1);
strSQL = "Update UserInfo Set Password = '"
+ EnPassword + "'"
+ " Where Name='" + name + "'";
try
{
ExecuteSql(strSQL);
return Password;
}
catch
{
throw new Exception("Get Password FAILED");
}
}
/// <summary>
/// Login
/// Need : Name、Password
/// </summary>
/// <returns>return bool</returns>
public bool Login()
{
strSQL = "Select Id from UserInfo Where Name='"
+ this.Name + "'"
+ " And Password='" + Functions.Encrypt(this.Password,1) +"'";
try
{
ExecuteSql4Value(strSQL);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Login
/// </summary>
/// <param name="name">User name</param>
/// <param name="password">Password</param>
/// <returns></returns>
public static bool Login(string name,string password)
{
strSQL = "Select Id from UserInfo Where Name='"
+ name + "'"
+ " And Password='" + Functions.Encrypt(password,1) +"'";
try
{
ExecuteSql4Value(strSQL);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Update user information
/// Need : Name、Mail、Password.
/// </summary>
/// <returns></returns>
public bool Update()
{
strSQL = "Update UserInfo Set "
+ "Mail='" + this.Mail
+"' Where Name='"+this.Name + "'"
+ " And Password='" + Functions.Encrypt(this.Password,1) +"'";
try
{
ExecuteSql(strSQL);
return true;
}
catch
{
throw new Exception("Update failed!");
}
}
/// <summary>
/// Update user information.
/// </summary>
/// <param name="name">Email(string)</param>
/// <param name="mail">User name(string)</param>
/// <param name="password">Password(string)</param>
/// <returns></returns>
public static bool Update(string mail,string name,string password)
{
strSQL = "Update UserInfo Set "
+ "Mail='" + mail
+"' Where Name='"+name + "'"
+ " And Password='" + Functions.Encrypt(password,1) +"'";
try
{
ExecuteSql(strSQL);
return true;
}
catch
{
throw new Exception("Update failed!");
}
}
/// <summary>
/// Get all the users
/// </summary>
/// <returns>return DataSet</returns>
public static DataSet GetUsers()
{
strSQL = "SELECT * FROM UserInfo";
try
{
return ExecuteSql4Ds(strSQL);
}
catch
{
throw new Exception("Get all the Users Information failed!");
}
}
/// <summary>
/// Get user info
/// </summary>
/// <returns></returns>
public bool GetUserInfo()
{
strSQL = "Select * from UserInfo Where Name='"
+ 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.Mail = reader.GetString(3);
return true;
}
else
{
return false;
}
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -