博客统计信息

51cto推荐博客
用户名:gaochaojs
文章数:131
评论数:265
访问量:187288
无忧币:654
博客积分:2482
博客等级:6
注册日期:2009-06-06

C#实现登录功能
2009-11-05 19:54:41
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://gaochaojs.blog.51cto.com/812546/222706
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)

 

本文出自 “南湖矿工技术空间” 博客,请务必保留此出处http://gaochaojs.blog.51cto.com/812546/222706

分享至
更多
一键收藏,随时查看,分享好友!
jingcha127
1人
了这篇文章
类别:经验总结技术圈()┆阅读()┆评论() ┆ 推送到技术圈返回首页

文章评论

 
2009-11-06 17:39:34
挺好的 学习学习。

 

发表评论            

【技术门诊】专家解析:软考重点难点及应试技巧
昵  称:
登录  快速注册
验证码:

请点击后输入验证码博客过2级,无需填写验证码

内  容: