使用LINQ从访问数据库设置登录页面 [英] Using LINQ to set up a login page from an access database

查看:80
本文介绍了使用LINQ从访问数据库设置登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Access数据库中有一张小记录表,其中包含4个人的记录,每个记录只有三个字段,即角色,用户名和密码.我试图建立一个登录页面,在这里我使用LINQ来读取用户名和密码字段,然后如果它们都正确,则根据该人在数据库中分配的角色来登录到4个可能的角色之一.我目前有这个.

I currently have a small table of 4 records of people in an Access Database each with just three fields, Role, Username and Password. Im trying to set up a login page where I'm using LINQ to read the Username and Password fields and then if they are both correct then to login to one of 4 possible roles depending on what role that person is allocated to on database. I currently have this.

       Dim strUsername As String
       Dim strPassword As String
       Dim strRole As String

       Dim query1 = From loginUsername In database1.tblStaff
                    Where loginUsername.FirstName = txtUsername.Text
                    Select loginUsername.FirstName

       Dim qeury2 = From loginPassword In database1.tblStaff
                    Where loginPassword.Password = txtPassword.Text
                    Select loginPassword.Password


   End Sub
End Class

如您所见,即时通讯试图获取用户在每个文本框中输入的内容并将其与数据库匹配,然后以某种方式设置if语句来检查其是否正确,但不确定我是否正确追踪.任何帮助都很好,谢谢.

As you can see, im trying to take what the user enters in each text box and match it to the database and then somehow set up an if statement checking if its right but im not sure if I'm even on the right track. Any help is great, thanks.

推荐答案

我想应该是这样的(免责声明:我对VB不熟悉)

I guess it should be something like this (disclaimer: I'm not familiar with VB)

Dim role = (From entry In database1.tblStaff
            Where entry .FirstName = txtUsername.Text
            AndAlso entry.Password = txtPassword.Text
            Select entry.Role)
           .FirstOrDefault()  'execute query explicitly

'if a role is null, then login or password incorrect
If role Is Nothing Then
    'No match
Else
    'Use role here
End If

没有必要分开查询.您可以将它们合并到一个查询中

There is no point to separate queries. You can merge them in a single query

这篇关于使用LINQ从访问数据库设置登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆