考勤管理系统实现登录

考勤登录系统主页面

  • 用职员的账户进行登录


    职员登录界面
  • 用管理员账户进行登录


    管理员账户界面

界面跳转代码

        {
            String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
            SqlConnection sqlConn = new SqlConnection(connStr);
            try
            {
                // 连接数据库
                sqlConn.Open();

                // 构造命令发送给数据库
                String sqlStr = "select * from employee where id=@id and password=@pwd";
                SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);

                // 注意是用用户ID登录,而不是用户名,用户名可能会重复
                cmd.Parameters.Add(new SqlParameter("@id", this.tb_User.Text.Trim()));
                cmd.Parameters.Add(new SqlParameter("@pwd", this.tb_Password.Text.Trim()));

                SqlDataReader dr = cmd.ExecuteReader();

                // 如果从数据库中查询到记录,则表示可以登录
                if (dr.HasRows)
                {
                    dr.Read();
                    UserInfo.userId = int.Parse(dr["id"].ToString());
                    UserInfo.userName = dr["name"].ToString();
                    UserInfo.userPwd = dr["password"].ToString();
                    UserInfo.userGender = dr["gender"].ToString();
                    UserInfo.userRole = dr["role"].ToString();
                    UserInfo.userDepartment = dr["department"].ToString();

                    MessageBox.Show(UserInfo.userRole + "登录成功");
                    
                    if (UserInfo.userRole == "职员")
                    {
                        // 显示员工主界面
                        Form2 Form = new Form2();
                        Form.Show();
                        // 隐藏登录界面
                        this.Hide();
                    }

                    if (UserInfo.userRole == "管理员")
                    {
                        
                        // 显示管理员主界面
                        Form3 formAdmin = new Form3();
                        formAdmin.Show();
                        // 隐藏登录界面
                        this.Hide();
                    }
                }
                else
                {
                    MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("访问数据库错误:" + exp.Message);
            }
            finally
            {
                sqlConn.Close();
            }
    
           
        }

配置引用数据库

<configuration>
    <connectionStrings>
        <add name="Attendance"
            connectionString="Data Source=.;Initial Catalog=考勤管理系统;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

创建UserInfo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    /// <summary>
    /// 登录用户信息
    /// </summary>
    class UserInfo
    {
        /// <summary>
        /// 记录登录ID
        /// </summary>
        public static int userId;
        /// <summary>
        /// 记录登录用户名
        /// </summary>
        public static String userName;
        /// <summary>
        /// 记录登录用户密码
        /// </summary>
        public static String userPwd;
        /// <summary>
        /// 记录用户性别
        /// </summary>
        public static String userGender;
        /// <summary>
        /// 记录用户所属部门
        /// </summary>
        public static String userDepartment;
        /// <summary>
        /// 记录登录用户类型
        /// </summary>
        public static String userRole;
    }
}

登录后的页面跳转

员工页面:


员工

管理员页面:


管理员
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。