Search code examples
c#winformsms-accessauthenticationoledb

C# Ms-access Getting details from database


I've created a login form using SQL, OLEDB Provider, which prompts the user for a name and password and if both match the username and password record in the Microsoft Access database then it directs them to a different form, however I don't know how to go about accessing all their information, e.g. Name, Surname and Age to display it as this obviously needs to be linked to who they have logged in as.

Can I link this to the name they've entered into the login text box if it's successful or is there a better way to load their details depending on who has logged in?


Solution

  • I would create 2 classes.One static class called Login that handles details of user logged in,rights assigned etc.Username and password will be passed to a method in this class, decrypted here,checked if correct and returns instance of User class:

    public static class Login{
     public static User LoggedUser {get; private set;}
     public static bool Authenticate(string userName, string password){
       //(a)Logic to fetch data based on username and pwd. mostly i use  Entityframework
       var user=context.Users.Find(x=>x.Name=userName && x.Pwd=MethodHelper.Decrypt(password));
       LoggedUser=user
       return LoggedUser != null;
     }
    }
    

    To use it: (1) Login form. if(Login.Authenticate(txtUserName.Text,txtPassword.Text){ //direct to another form if true. Just by doing this it also sets the loggedUser property of this class. }else{ //access denied logic}

    (2)Other scenarios like display name on mainform status bar.

    statusBarLabelUserName.Text=Login.LoggedUser.Name;
    statusBarLabelSurname.Text=Login.LoggedUser.surname;