简体   繁体   English

C#Ms-access从数据库获取详细信息

[英]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, eg Name, Surname and Age to display it as this obviously needs to be linked to who they have logged in as. 我已经使用SQL OLEDB Provider创建了一个登录表单,该表单提示用户输入名称和密码,如果两者都与Microsoft Access数据库中的用户名和密码记录匹配,则它将它们定向到其他表单,但是我不这样做知道如何访问他们的所有信息,例如姓名,姓氏和年龄来显示它,因为显然这需要与他们登录时的身份相关联。

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? 如果成功,是否可以将其链接到他们在登录文本框中输入的名称,还是有更好的方法来加载其详细信息(取决于谁登录)?

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: 我将创建2个类。一个名为Login的静态类处理用户登录的详细信息,分配的权限等。用户名和密码将传递给此类中的方法,在此解密,检查是否正确并返回User类的实例:

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. 要使用它:(1)登录表单。 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} if(Login.Authenticate(txtUserName.Text,txtPassword.Text){//如果为true,则直接指向另一种形式。仅通过此操作,它还将设置此类的loggingUser属性。} else {//访问被拒绝的逻辑}

(2)Other scenarios like display name on mainform status bar. (2)其他情况,例如在主窗体状态栏上显示名称。

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

Depending on the structure of your Access db, and the tables involved, probably the personal details will be associated with some sort of unique user ID. 根据您的Access数据库的结构以及所涉及的表,个人详细信息可能会与某种唯一的用户ID相关联。

That's what you should be using to get their data. 那就是你应该用来获取他们的数据的东西。 When login is successful, you should use the userID to get the details you need. 登录成功后,您应该使用userID来获取所需的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM