2014-01-11 4 views
1

로그인 양식에서 DOCTOR 테이블에있는 잭으로 로그인하면 page_two로 이동합니다. 잭이 간호사가 아니라 의사이기 때문에 간호사 버튼 1과 간호사 버튼 2를 비활성화하고 싶습니다. 그런 다음 NURSE 테이블에있는 Mary로 로그인하면 반대쪽으로 page_two로 이동합니다. 메리가 의사가 아니라 간호사이기 때문에 의사 버튼 1과 의사 버튼 2를 비활성화하고 싶습니다. 로그인 한 사용자에 따라 특정 버튼을 비활성화하십시오.

Page_two의 버튼 이름

난 당신이 사람이 데이터를 보유하기 위해 일부 Person 클래스를 생성하는 것이 좋습니다 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]pwd;"; 
      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 : 컨트롤을 활성화 또는 비활성화 할 사람이 로그인의 기본 폼의 사용 위치에

Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); LoginForm loginForm = new LoginForm(); if (loginForm.ShowDialog() != DialogResult.OK) return; Application.Run(new MainForm(loginForm.Person)); 

: 그것은 MainForm 생성자에 전달해야한다 Person 인스턴스를 얻을 수 PersonRepository를 사용해야합니다. Visible = False; '를 선택합니다. –

+0

좋아요.하지만 잭으로 의사로 로그인 할 수는 없지만, nursebutton을 클릭 할 때 액세스 거부 메시지가 튀어 나오면 어떨까요? @ Al-3sli – Pony

+0

당신이 공개 변수를 작성하여 의사가 투과 타입을 가질 수 있습니다. 의사가 다른 1, 2와 같이 설정하면 다음과 같습니다 :'permeationtype = 1;'이 사용자가 의사임을 나타내므로 사용자가 간호사 버튼을 누르면 투과 유형 1이면 메시지를 계속 보여주세요. –

답변

1

입니다 :

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

여기에서 Position은 사용자의 작업 가능 위치에 대한 열거 형입니다 NS :

public enum JobPosition 
{ 
    Doctor, 
    Nurse 
} 

이 다음 단계는 어떤 저장소 클래스에 데이터베이스 쿼리를 이동하여 프리젠 테이션 코드에서 데이터 액세스 로직을 분리됩니다

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

이 또한 내가 별도의 로그인 양식을 만들 것 전에 표시되어야 당신의 기본 양식이 시작됩니다.

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 
    } 
} 
관련 문제