2014-01-11 54 views
1

在登录表单,当我登录杰克存在于医生表,它会去page_two。我想禁用护士按钮1,和护士按钮2,因为杰克是不是一个护士,但医生。然后换相反的,如果我登录玛丽,存在于护士表,它会去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 [email protected] AND [email protected];"; 
      strCommandtext += "SELECT nUsername, nPassword from NURSE WHERE [email protected] AND [email protected];"; 
      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

使用'form.nursebtn。 Visible = False;'当你创建表单实例时。 –

+0

好,不过如果我湾登陆的医生是千斤顶,但是怎么样,如果我想点击nursebutton时蹦出来否认消息的访问? @铝3sli – Pony

+0

您可以创建公共变量来保存渗透型,如果是医生设置1,否则2,是这样的:'permeationtype = 1;'这表明该用户是医生,所以当用户按下nursebutton您可以检查permeationtype如果它是1,那么显示你的消息,否则继续。 –

回答

1

我建议你创建一些Person类持有人数据:

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

哪里Position是岗位提供一个枚举您perso NS:

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 
    } 
}