C#实现登录功能
FrmLogin.ca文件
/* 注释Manual
* 文件功能描述只需简述,具体详情在类的注释中描述。
* 创建标识和修改标识由创建或修改人员的拼音或英文名加日期组成。如:
* 一天内有多个修改的只需做一个在注释说明中做一个修改标识就够了。
* 在所有的代码修改处加上修改标识的注释。
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XEP.Business;
using XEP.Util;
namespace XEP.UI
{
public partial
class FrmLogin : Form
{
private FrmNewMain frmNewMain;
public FrmLogin()
{
InitializeComponent();
this.CenterToScreen();
}
//tfq-7-4---------------------------
public FrmLogin(FrmNewMain frmNewMain)
{
this.frmNewMain = frmNewMain;
InitializeComponent();
this.CenterToScreen();
}
//tfq----------------------------------
protected
void InitUI()
{
this.FormBorderStyle = FormBorderStyle.None;
//this.BackgroundImage = new Image();
}
private
void btnLogin_Click(
object sender, EventArgs e)
{
string id = txtEMPID.Text.Trim();
if (id.Contains(
"'")||id.Contains(
"=")||id.Contains(
"is")||id.Contains(
"true")||id.Contains(
"@"))
{
MessageBox.Show(
"输入工号违法");
return;
}
//判断全角半角
if (id.Length != System.Text.Encoding.Default.GetByteCount(id))
{
MessageBox.Show(
"不允许全角输入",
"输入错误");
return;
}
if (id =="")
{
MessageBox.Show(
"输入工号");
return;
}
else
if(txtPWD.Text =="")
{
if (MessageBox.Show(
"密码为空,继续登录吗?") == DialogResult.No)
{
return;
}
}
if (EmployeeBLL.CheckEmployeePassword(id, txtPWD.Text.Trim()))
{
//new FrmMain(id).Show();
//tfq-7-03
GlobalProperties.ScheEmpID = id;
new FrmProscenium(id,frmNewMain).Show();
this.Hide();
//this.Close();
frmNewMain.resetStateStrip();
}
else
{
MessageBox.Show(
"工号密码不匹配,注意是否打开大小写锁定键了?");
return; ;
}
}
private
void btnReset_Click(
object sender, EventArgs e)
{
this.txtEMPID.Text = "";
txtPWD.Text = "";
}
private
void btnExit_Click(
object sender, EventArgs e)
{
this.Close();
}
private
void txtPWD_KeyPress(
object sender, KeyPressEventArgs e)
{
}
private
void txtPWD_Enter(
object sender, EventArgs e)
{
}
private
void FrmLogin_FormClosing(
object sender, FormClosingEventArgs e)
{
frmNewMain.Show();
}
private
void txtPWD_TextChanged(
object sender, EventArgs e)
{
}
private
void groupBox1_Enter(
object sender, EventArgs e)
{
}
}
}
EmplyeeDAL.cs
/*----------------------------------------------------------------/
Copyright (C)
文件名:
文件功能描述:
创建标识:
修改标识:
修改描述:
/----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using XEP.Model;
namespace XEP.DataAccess
{
public
class EmployeeDAL
{
/// <summary>
/// 验证用户登陆信息
/// </summary>
/// <param name="empid">员工号</param>
/// <param name="pwd">密码(MD5加密后)</param>
/// <returns></returns>
public
static
bool CheckPassword(
string empid,
string pwd)
{
string sql =
string.Format(
"select EmpID from employee where empid = '{0}' and password = '{1}'",empid,pwd);
if (DBAccess.getExecuteCount(sql) > 0)
{
return
true;
}
else
{
return
false;
}
}
///<summary>
///通过工号获取员工姓名
///</summary>
///<param name="empID" >员工号</parm>
///<returns>employeeName</returns>
public
static
string FindEmployeeByID(
int empID)
{
string employeeName =
null;
SqlDataReader reader = DBAccess.getDataReader(
"SELECT EmpName FROM employee WHERE empID='" +empID+
"' ");
if (reader.Read())
{
employeeName =reader.GetString(0);
}
DBAccess.close();
return employeeName;
}
public
static
bool IsHasRight(
short empLevel,
string empID)
{
string selectSql =
string.Format(
"SELECT * FROM Discount,Employee WHERE EmpID='{0}' AND Employee.Emplevel >= {1}", empID, empLevel);
if (DBAccess.getExecuteCount(selectSql)> 0)
{
return
true;
}
else
{
return
false;
}
}
public
static
short GetEmpLevel(
string empID)
{
string sql =
string.Format(
"select EmpLevel from employee where empid = '{0}' ", empID);
return Convert.ToInt16(DBAccess.selectFirstRecord(sql));
}
public
static
double GetEmpDiscount(
string empID)
{
string sql =
string.Format(
"select EmpDiscount from employee where empid = '{0}' ", empID);
return Convert.ToDouble(DBAccess.selectFirstRecord(sql));
}
public
static
string GetRightCode(
string empID)
{
string sql =
string.Format(
"select UserRightCode from employee where empid = '{0}' ", empID);
return Convert.ToString(DBAccess.selectFirstRecord(sql));
}
//tfq-7-3
public
static
string GetEmpNameByEmpID(
string empid)
{
string select =
"select EmpName from Employee where EmpID='" +
empid +
"'";
DataTable dt = DBAccess.getDataTable(select);
if (dt.Rows.Count > 0)
{
string result = dt.Rows[0][0].ToString();
return result;
}
else
{
return "";
}
}
/// <summary>
/// 获得所有员工的基本信息(工号、姓名)
/// </summary>
/// <returns>所有员工的信息(工号、姓名)</returns>
public
static List<Employee> GetEmployee()
{
List<Employee> listEmployee =
new List<Employee>();
Employee emp =
null;
string selectSql =
"SELECT EmpID,EmpName FROM Employee";
SqlDataReader reader = DBAccess.getDataReader(selectSql);
while (reader.Read())
{
emp =
new Employee();
emp.EmpID = reader.GetString(0);
emp.EmpName = reader.GetString(1);
listEmployee.Add(emp);
}
reader.Close();
DBAccess.close();
return listEmployee;
}
}
}
注:
MessageBoxResult
指定用户单击了哪个消息框按钮。MessageBoxResult 由 Show 方法返回。
成员名称说明:
None 消息框未返回值。
OK 消息框的结果值为“确定”。
Cancel 消息框的结果值为“取消”。
Yes 消息框的结果值为“是”。
No 消息框的结果值为“否”。
Show 返回一个 MessageBoxResult 值,该值指定用户单击了消息框上的哪个按钮。MessageBoxButton 指定消息框上显示哪些按钮。从消息框返回的结果值取决于消息框上有什么按钮,以及用户如何关闭消息框:在包含“确定”按钮的消息框上,如果用户单击“确定”按钮、单击标题栏中的“关闭”按钮或按 Esc 键,则会返回 OK。在包含“确定”按钮和“取消”按钮的消息框上,如果用户单击“确定”按钮,则会返回 OK。如果用户单击“取消”按钮或标题栏中的“关闭”按钮,则会返回 Cancel。在包含“是”按钮和“否”按钮的消息框上,标题栏中的“关闭”按钮处于禁用状态。因此,如果用户单击“是”按钮,则会返回 Yes,如果用户单击“否”按钮,则会返回 No。在包含“是”按钮、“否”按钮和“取消”按钮的消息框上,如果单击了“是”按钮,则会返回 Yes,如果单击了“否”按钮,则会返回 No。如果用户单击“取消”按钮或标题栏中的“关闭”按钮,则会返回 Cancel。
System.Collections.Generic 命名空间包含定义泛型集合的接口和类,泛型集合允许用户创建强类型集合,它能提供比非泛型强类型集合更好的类型安全性和性能。
类
id.Length != System.Text.Encoding.Default.GetByteCount(id)
本文转自 gaochaojs 51CTO博客,原文链接:http://blog.51cto.com/jncumter/222706,如需转载请自行联系原作者