根据用户登录的情况禁用某些按钮

时间:2014-01-11 09:45:23

标签: c# windows forms button login

在登录表单中,当我作为Jack存在于DOCTOR表中时,它将转到page_two。我想禁用护士按钮1和护士按钮2,因为杰克不是护士而是医生。然后相反,如果我作为Mary登录,它存在于NURSE表中,它将转到page_two。我想禁用医生按钮1和医生按钮2,因为玛丽不是医生而是护士。

Page_two的按钮名称是btnDoctor1,btnDoctor2,btnNurse1和btnNurse2

//登录表单代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace GRP_02_03_SACP
{
    public partial class page_one : Form
    {
        public page_one()
        {
            InitializeComponent();

        }


        private void page_one_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            //retrieve connection information info from App.config
            string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnection"].ConnectionString;
            //STEP 1: Create connection
            SqlConnection myConnect = new SqlConnection(strConnectionString);
            //STEP 2: Create command
            string strCommandtext = "SELECT dUsername, dPassword from DOCTOR";
            // Add a WHERE Clause to SQL statement
            strCommandtext += "   WHERE dUsername=@dname AND dPassword=@dpwd;";
            strCommandtext += "SELECT nUsername, nPassword from NURSE WHERE nUsername=@nname AND nPassword=@npwd;";
            SqlCommand cmd = new SqlCommand(strCommandtext, myConnect);
            cmd.Parameters.AddWithValue("@dname", textUsername.Text);
            cmd.Parameters.AddWithValue("@dpwd", txtPassword.Text);
            cmd.Parameters.AddWithValue("@nname", textUsername.Text);
            cmd.Parameters.AddWithValue("@npwd", txtPassword.Text);


            try
            {
                // STEP 3: open connection and retrieve data by calling ExecuteReader
                myConnect.Open();
                // STEP 4: Access Data
                SqlDataReader reader = cmd.ExecuteReader();


                while (reader.Read()) //For Doctor
                {
                    if (MessageBox.Show("Login Successful") == DialogResult.OK)
                    {
                        page_two form = new page_two();
                        form.Show();
                        return;
                    }                                     
                } 
                reader.NextResult();
                while (reader.Read()) //For Nurse
                {
                    if (MessageBox.Show("Login Successful") == DialogResult.OK)
                    {
                        page_two form = new page_two();
                        form.Show();
                        return;
                    }
                }

                //STEP 5: close connection
                reader.Close();
                MessageBox.Show("Invalid username or password");
            }
            catch (SqlException ex)
            {

            }
            finally
            {
                //STEP 5: close connection
                myConnect.Close();
            }
        }      
    }
}

1 个答案:

答案 0 :(得分:1)

我建议您创建一些Person类来保存人员数据:

public class Person
{
    public string Name { get; set; }
    public JobPosition Position { get; set; }
    // etc
}

其中Position是您的人员可以获得的工作职位:

public enum JobPosition
{
    Doctor,
    Nurse
}

下一步是将数据访问逻辑与表示代码分离,方法是将数据库查询移动到某个存储库类:

public class PersonRepository
{
    public Person GetPerson(string userName, string password)
    {
        // execute query and create Person instance
    }
}

此外,我还会创建单独的登录表单,该表单应在主表单开始之前显示。它应该使用PersonRepository来获取应传递给Person构造函数的MainForm实例:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

LoginForm loginForm = new LoginForm();
if (loginForm.ShowDialog() != DialogResult.OK)
    return;

Application.Run(new MainForm(loginForm.Person));

在主要表单上使用登录人员的位置来启用或禁用控件:

public partial class MainForm : Form
{
    private Person _person;

    public MainForm(Person person)
    {
        InitializeComponent();
        _person = person;
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        fooButton.Enabled = (_person.Position == JobPosition.Doctor);
        // etc
    } 
}