2013-04-10 145 views
1

我使用Visual Studio 2010中的角色列值和Windows窗體在C#應用程序。 該應用程序正常工作我可以完美登錄,並獲取用戶名登錄到下一個表單,這是登錄成功後打開的菜單表單。 現在我的問題是我要檢查登錄的用戶是否是經理還是取決於存儲在數據庫中的角色列中的值管理,這樣的話我可以禁用Admin用戶的某些功能。 任何想法或幫助指引我到正確的地方嗎? 我希望我的問題是清楚的從數據庫中檢索登錄後

我有一個表叫向tblUsers在那裏我有以下字段:

userId 
Username 
Password 
FirstName 
LastName 
Mobile 
Landline 
Address 
Email 
Role 

// clsLoginCollection:

 public Boolean Login(clsLogin Login) 
     { 
     clsDataConduit logincheck = new clsDataConduit(); 
     logincheck.AddParameter("Username", Login.username); 
     logincheck.AddParameter("Password", Login.password); 

     logincheck.Execute("sproc_tblUser_Login"); 

     if (logincheck.Count > 0) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

//存儲過程:

ALTER PROCEDURE sproc_tblUser_Login 
@Username varchar(10), 
@Password varchar(10) 

AS 

select Username, Password from tblUsers where Username = @Username AND Password = @Password 

// clsLoginform:

private void btnLogin_Click(object sender, EventArgs e) 
    { 
     Boolean b = false; 

     if ((String.IsNullOrEmpty(tbUsername.Text)) || (String.IsNullOrEmpty(tbPassword.Text))) 
     { 
      MessageBox.Show("Username or Password Cannot Be Blank.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     } 
     else 
     { 
      clsLoginCollection l = new clsLoginCollection(); 
      clsEncryption encrypt = new clsEncryption();// this class have the encryption method 
      b = l.Login(new clsLogin(tbUsername.Text, encrypt.Encrypt(tbPassword.Text))); 

      if (b == true) 
      { 
       MainMenu m = new MainMenu(tbUsername.Text, tbPassword.Text); 
       m.Show(); 
       this.Visible = false; 
      } 
      else 
      { 
       MessageBox.Show("Invalid Login Details", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2); 
      } 
     } 
    } 

// clsLogin完全

public class clsLogin 
{ 
    private Int32 UserID; 
    private string Username; 
    private string Password; 
    private string FirstName; 
    private string LastName; 
    private string Mobile; 
    private string Landline; 
    private string Address; 
    private string Email; 
    private string Role; 

    public clsLogin() 
    { } 

    public clsLogin(string UserName, string Password) 
    { 
     this.username = UserName; 
     this.Password = Password; 
    } 

    public clsLogin(string UserName, string Password, string FirstName, string LastName, string Mobile, string Landline, string Address, string Email, string Role) 
    { 
     this.username = UserName; 
     this.Password = Password; 
     this.FirstName = FirstName; 
     this.LastName = LastName; 
     this.Mobile = Mobile; 
     this.Landline = Landline; 
     this.Address = Address; 
     this.Email = Email; 
     this.Role = Role; 
    } 

    public Int32 userID 
    { 
     get { return UserID; } 
     set { UserID = value; } 
    } 

    public string username 
    { 
     get {return Username;} 
     set {Username = value;} 
    } 

    public string password 
    { 
     get {return Password;} 
     set {Password = value;} 
    } 

    public string firstName 
    { 
     get { return FirstName; } 
     set { FirstName = value; } 
    } 

    public string lastName 
    { 
     get { return LastName; } 
     set { LastName = value; } 
    } 

    public string mobile 
    { 
     get { return Mobile; } 
     set { Mobile = value; } 
    } 

    public string landline 
    { 
     get { return Landline; } 
     set { Landline = value; } 
    } 

    public string address 
    { 
     get { return Address; } 
     set { Address = value; } 
    } 

    public string email 
    { 
     get { return Email; } 
     set { Email = value; } 
    } 

    public string role 
    { 
     get { return Role; } 
     set { Role = value; } 
    } 
} 

回答

0

這是我回答這個問題我也問過。我已經使用RIKKI提供b

// clsLoginCollection查詢(補充說,返回的作用的新方法)

public string CheckRole(clsLogin Rolecheck) 
    { 
     string Role; 
     clsDataConduit dataconduit = new clsDataConduit(); 
     dataconduit.AddParameter("@Username", Rolecheck.username); 
     dataconduit.AddParameter("@Password", Rolecheck.password); 
     dataconduit.Execute("sproc_tblUsers_FilterByRole"); 

     Role = dataconduit.QueryResults.Rows[0]["Role"].ToString(); 

     return Role; 
    } 

和我Login.cs類,我創建了一個字符串變量來保存結果此方法

string role = "" 

和下方的

logindetails = l.Login(new clsLogin(tbUsername.Text, encrypt.Encrypt(tbPassword.Text))); 

的我已經加入此代碼

roleofuser = l.CheckRole(new clsLogin(tbUsername.Text, encrypt.Encrypt(tbPassword.Text))); 

所以這個「roleofuser」會,如果用戶是管理員,經理等不同的角色,並返回然後用if/else語句的IAM能夠將它們重定向到正確的形式

感謝 剛萬一有人被困在這個特定的問題

0

像這樣的事情?

SELECT Role FROM tblUsers WHERE Username = @Username 
+0

我已經得到了SQL查詢,我可以登錄通過檢查用戶名和密碼,但現在我想要做的是當我登錄檢索存儲在角色列中的值檢查用戶是管理員還是管理員,如果是管理員,則重定向到管理員表單,如果是管理員,則是管理員表單 蔭使用C#在Visual Studio 2010中,這是一個Windows窗體應用程序 – user2267472 2013-04-10 23:03:23

+0

哎呀sowi didnt閱讀烏爾查詢正確RIKKI乙 sowi IAM要去嘗試,在一個位 – user2267472 2013-04-10 23:04:51

+0

@ user2267472是查詢檢查用戶和通過。您需要另一個查詢來查找該用戶的角色。希望這一個適合你。您的幫助 我仍然沒有管理 – 2013-04-10 23:12:07