[英]How do I check if a user has a certain role in their profile?
Good-day, 美好的一天,
I need some help. 我需要协助。 I'm trying to query my MySQL database to see if the currently logged on user to my application has certain rights/role. 我正在尝试查询我的MySQL数据库,以查看当前登录到我的应用程序的用户是否具有某些权限/角色。 If the queried value does exist, then I want to enable a certain menu item. 如果查询的值确实存在,那么我要启用某个菜单项。
I checked out most of the similar questions as I was typing my question, but they mostly deal with ASP and JSP so that confuses me even further (as I haven't studied those yet). 在键入问题时,我检查了大多数类似的问题,但它们大多涉及ASP和JSP,这使我更加困惑(因为我还没有研究过这些问题)。 I'm still learning VB.Net and MySQL. 我仍在学习VB.Net和MySQL。
Your help would be greatly appreciated. 您的帮助将不胜感激。
Here's my code - what am I not doing right?: 这是我的代码-我在做什么不对?
Public Sub checkAccessLevel()
Dim dbConn As New MySqlConnection(String.Format("Server={0};Port={1};Uid={2};Password={3};Database=parts", FormLogin.ComboBoxServerIP.SelectedItem, My.Settings.DB_Port, My.Settings.DB_UserID, My.Settings.DB_Password))
Dim dbQuery As String = "SELECT Level FROM users WHERE username = '" & FormLogin.TextBoxUsername.Text & "'"
Dim dbAdapter As New MySqlDataAdapter(dbQuery, dbConn)
Dim dbData As MySqlDataReader
Try
dbConn.Open()
dbData = dbAdapter.SelectCommand.ExecuteReader
dbData.Read()
While dbData.Read
Select Case UCase(dbData(0).ToString)
Case Is = "Admin"
TSMenuItemOptions.Enabled = True
Case Is = "Manager"
TSMenuItemOptions.Enabled = True
Case Is = "User"
TSMenuItemOptions.Enabled = False
End Select
End While
dbData.Close()
Catch ex As Exception
MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
Finally
dbAdapter.Dispose()
dbConn.Close()
End Try
End Sub
I managed to resolve my own question as follows; 我设法解决了自己的问题,如下所示;
By modifying the Select Case statement.... 通过修改Select Case语句...。
ORIGINAL (Incorrect): 原始(不正确):
Select Case UCase(dbData(0).ToString)
MODIFIED (Correct): 修改(正确):
Select Case dbData(0).ToString
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.