Vb.net Visual Basic 2012中的多用户登录问题
我有以下代码,这意味着根据登录凭据显示不同的页面。例如,以管理员身份登录应该显示AdminPanel,而以其他人身份登录应该显示UserPanel。问题是,无论您以谁的身份登录,它都会显示UserPanelVb.net Visual Basic 2012中的多用户登录问题,vb.net,visual-studio-2012,Vb.net,Visual Studio 2012,我有以下代码,这意味着根据登录凭据显示不同的页面。例如,以管理员身份登录应该显示AdminPanel,而以其他人身份登录应该显示UserPanel。问题是,无论您以谁的身份登录,它都会显示UserPanel Try Dim connection As New SqlClient.SqlConnection Dim command As New SqlClient.SqlCommand Dim adaptor As New SqlClient
Try
Dim connection As New SqlClient.SqlConnection
Dim command As New SqlClient.SqlCommand
Dim adaptor As New SqlClient.SqlDataAdapter
Dim dataset As New DataSet
connection.ConnectionString = ("Data Source=.\SQLEXPRESS;Initial Catalog=FSMembers;Integrated Security=True;Pooling=False")
command.CommandText = "select * from [users] where username='" & UsernameTextBox.Text & "' and password ='" & PasswordTextBox.Text & "' and Position='admin or user' " '
connection.Open()
command.Connection = connection
adaptor.SelectCommand = command
adaptor.Fill(dataset, "0")
Dim count = dataset.Tables(0).Rows.Count
If count < 1 Then
UserPanel.Show()
Me.Hide()
ElseIf count > 1 Then
AdminPanel.Show()
Me.Hide()
Else
MsgBox("You have inserted invalid Login details." & vbNewLine & "Please try again!", MsgBoxStyle.Critical, "Login Failed | FS Members")
UsernameTextBox.Clear()
PasswordTextBox.Clear()
End If
Catch ex As System.Data.SqlClient.SqlException
MsgBox(ex.Message)
End Try
试试看
Dim连接作为新的SqlClient.SqlConnection
Dim命令作为新的SqlClient.SqlCommand
Dim适配器作为新的SqlClient.SqlDataAdapter
将数据集设置为新数据集
connection.ConnectionString=(“数据源=。\SQLEXPRESS;初始目录=FSMembers;集成安全性=True;池=False”)
command.CommandText=“从[users]中选择*,其中username='”&UsernameTextBox.Text&“'和PasswordTextBox.Text&'”和Position='admin或user''
connection.Open()
command.Connection=Connection
adapter.SelectCommand=命令
适配器填充(数据集“0”)
Dim count=dataset.Tables(0).Rows.count
如果计数小于1,则
UserPanel.Show()
我躲起来
如果计数大于1,则
AdminPanel.Show()
我躲起来
其他的
MsgBox(“您插入了无效的登录详细信息。”&vbNewLine&“请重试!”,MsgBoxStyle.Critical,“登录失败| FS成员”)
UsernameTextBox.Clear()
PasswordTextBox.Clear()
如果结束
捕获ex为System.Data.SqlClient.SqlException
MsgBox(例如消息)
结束尝试
希望有人能给我指出正确的方向
谢谢,
丹这样做:
Try
Dim connection As New SqlClient.SqlConnection
Dim command As New SqlClient.SqlCommand
command.CommandText = "SELECT Position from [users] WHERE username='" & UsernameTextBox.Text & "' and password ='" & PasswordTextBox.Text & "'"
connection.Open()
command.Connection = connection
Dim Res = command.ExecuteScalar()
If Res IsNot Nothing Then
If Res.ToString().ToLower() <> "admin" Then
UserPanel.Show()
Me.Hide()
Else
AdminPanel.Show()
Me.Hide()
End If
Else
MsgBox("You have inserted invalid Login details." & vbNewLine & "Please try again!", MsgBoxStyle.Critical, "Login Failed | FS Members")
UsernameTextBox.Clear()
PasswordTextBox.Clear()
End If
Catch ex As System.Data.SqlClient.SqlException
MsgBox(ex.Message)
End Try
试试看
Dim连接作为新的SqlClient.SqlConnection
Dim命令作为新的SqlClient.SqlCommand
command.CommandText=“从[users]中选择位置,其中username='”&UsernameTextBox.Text&“'和PasswordTextBox.Text&'”
connection.Open()
command.Connection=Connection
Dim Res=command.ExecuteScalar()
如果Res不是什么,那么
如果Res.ToString().ToLower()“admin”,则
UserPanel.Show()
我躲起来
其他的
AdminPanel.Show()
我躲起来
如果结束
其他的
MsgBox(“您插入了无效的登录详细信息。”&vbNewLine&“请重试!”,MsgBoxStyle.Critical,“登录失败| FS成员”)
UsernameTextBox.Clear()
PasswordTextBox.Clear()
如果结束
捕获ex为System.Data.SqlClient.SqlException
MsgBox(例如消息)
结束尝试
我是用SO编辑器写的,所以可能会有一些小的打字错误。最重要的是,我希望您了解,这种构造查询的方式会使您的应用程序受到攻击。您应该真正考虑为它创建存储过程,或者使用一些应用程序级别的方法将参数发送到查询,而不是将文本框值连接到字符串中。排序
我更改了以下内容:
If Res IsNot Nothing Then
If Res.ToString().ToLower() <> "admin" Then
UserPanel.Show()
Me.Hide()
Else
AdminPanel.Show()
Me.Hide()
End If
如果Res不是空的,那么
如果Res.ToString().ToLower()“admin”,则
UserPanel.Show()
我躲起来
其他的
AdminPanel.Show()
我躲起来
如果结束
致:
如果Res不是空的,那么
如果Res.ToString().ToLower()“admin”,则
AdminPanel.Show()
我躲起来
其他的
UserPanel.Show()
我躲起来
如果结束
现在可以了。
非常感谢你。非常感谢您的帮助。您的查询很可能没有达到您认为的效果。最后一部分
和Position='admin或user'““
会引起一些人的注意。哈哈,我知道代码没有做我希望它做的事情,也没有做我认为它正在做的事情,亨斯我的帖子:)Position是一个数据库列。所以,为什么不直接在ManagementStudio中运行查询,甚至在VS中运行查询,看看是否得到了预期的结果呢?我们不知道您的数据库结构,也不知道您如何存储用户类型,所以我们永远无法确定。我知道了。。。。这就是我在这里发帖的原因。它只是登录。没有错误或任何东西。只是没有根据与我一起登录的用户登录到正确的位置。告诉我一件事。在Position
列中存储什么?我猜可能是“管理员”或“用户”,但不是“管理员”或“用户”,或者是“管理员”或“用户”?好吧,伙计,这很管用,但情况正好相反。管理员正在登录UserPanel,用户正在登录AdminPanel。非常感谢您的帮助。但是这仍然和以前一样(反向访问)在上放置一个断点,如果Res.ToString().ToLower()“admin”,那么
行并告诉我你在Res
FSMembers.exe中得到了什么值!FSMembers.FormLogin.btnLogin_单击(Object sender,System.EventArgs e)第37行基本字符串“0”字符串,这是我从中得到的所有字符串。如果您以前没有这样做,请将(键盘)光标移到该行(如果Res.ToString().ToLower()“admin”,则按)并按F9
。这将使线条背景变成棕色,表示在该线条上设置了断点。现在运行代码。代码将在该行停止,您将被带到VS。将鼠标移到Res
变量,工具提示将显示Res的当前值。您看到了什么?您的系统有问题。上面的代码不应该工作。如果用户类型不是admin,则要求它显示admin面板。希望你能尽快解决这个问题,你是对的。工作完成后,我发现有些地方出了问题,并发现数据库表由于某种奇怪的原因前后颠倒。现在使用您的代码对所有内容进行排序。再次非常感谢。我的互联网昨晚关闭了,所以当时无法回复,我无法投票支持答案。但我真诚地感谢你。
If Res IsNot Nothing Then
If Res.ToString().ToLower() <> "admin" Then
AdminPanel.Show()
Me.Hide()
Else
UserPanel.Show()
Me.Hide()
End If