我无法连接到我的数据库,我收到消息"无效的用户或密码"即使密码是正确的,我应该做什么任何想法或我的代码错在这里是我的登录表单的代码/
这是我的代码
Imports System.Data.SqlClient
Public Class LoginForm
Private Sub UsersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles UsersBindingNavigatorSaveItem.Click
Me.Validate()
Me.UsersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DataSet1)
End Sub
Private Sub LoginForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DataSet1.users' table. You can move, or remove it, as needed.
Me.UsersTableAdapter.Fill(Me.DataSet1.users)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connection As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database_topdent.mdf;Integrated Security=True")
Dim command As New SqlCommand("Select * from users where User = @user and Password = @password ", connection)
command.Parameters.Add("@user", SqlDbType.VarChar).Value = UserTextBox.Text
command.Parameters.Add("@password", SqlDbType.VarChar).Value = PasswordTextBox.Text
Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()
adapter.Fill(table)
If table.Rows.Count() <= 0 Then
MessageBox.Show("username or textbox invalid")
Else
Form1.Show()
Me.Hide()
End If
End If
End Sub
End Class
答案 0 :(得分:0)
这太令人困惑了。 您需要检查用户名和密码,而不仅仅是计算行数。那个table.rows.count“&lt; = 0”?对我毫无意义。 我可以看到你在哪里,但它错了。而问题本身并不够清楚。但假设这是一个登录表单。试试这个&gt;
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1_Click
Dim connection As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database_topdent.mdf;Integrated Security=True")
Dim command As New SqlCommand("Select * from [users] where [User] = @1 and [Password] = @2 ", connection)
connection.Open()
command.Parameters.AddWithValue("@1", UserTextBox.Text)
command.Parameters.AddWithValue("@2", PasswordTextBox.Text)
Using dr As SqlDataReader = command.ExecuteReader
Dim userFound As Boolean = False
Dim Name As String = ""
While dr.Read
userFound = True
Name = dr("User").ToString
End While
If userFound = True Then
MsgBox("Yes!")
ElseIf userFound = False Then
MsgBox("nay")
End If
End Using
connection.close()
End Sub
只需确保您的列设置为vchar即可。我无法测试它,但它应该可以工作。
首先,您需要打开连接。然后,您需要使用阅读器读取数据库中的数据。然后,如果找到用户名和密码,则显示一个msg或任何你想要的内容。 我确信有更好的方法可以做到这一点,但在这里,我正在扮演一个快速好的撒玛利亚人。
更新#With权限 1.添加另一个名为“Permissions”的列 2.创建一个模块。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1_Click
Dim connection As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database_topdent.mdf;Integrated Security=True")
Dim command As New SqlCommand("Select * from [users] where [User] = @1 and [Password] = @2 ", connection)
connection.Open()
command.Parameters.AddWithValue("@1", UserTextBox.Text)
command.Parameters.AddWithValue("@2", PasswordTextBox.Text)
Using dr As SqlDataReader = command.ExecuteReader
Dim userFound As Boolean = False
While dr.Read
userFound = True
User1 = dr("User").ToString
Permissions = dr("Permissions").ToString
End While
If userFound = True Then
MsgBox("Yes!")
ElseIf userFound = False Then
MsgBox("nay")
End If
End Using
connection.close()
End Sub
模块
Module Module1
Public User1 As String
Public Permissions As String
End Module
然后在表单中调用内容... 例如,在您的主窗体或任何地方,如果您在加载表单上有2个按钮,则可以执行
If Permissions = "admin" then
Button1.enabled = true
else if Permissions = "user" then
msgbox(fu)
end if
答案 1 :(得分:0)
你好这里是我的登录代码,管理员/用户可以使用,但是知道我想在用户登录时做出权限示例,但没有完全访问权限,但管理员是完全访问权限。
以下是“Login_Form”
的所有代码Imports System.Data.SqlClient
Module Module1
Public User1 As String
Public Permissions As String
End Module
Public Class LoginForm
Private Sub UsersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles UsersBindingNavigatorSaveItem.Click
Me.Validate()
Me.UsersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DataSet1)
End Sub
Private Sub LoginForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DataSet1.users' table. You can move, or remove it, as needed.
Me.UsersTableAdapter.Fill(Me.DataSet1.users)
If Permissions = "admin" Then
Button1.Enabled = True
ElseIf Permissions = "user" Then
Form1.RaportetToolStripMenuItem.Visible = False
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connection As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database_topdent.mdf;Integrated Security=True")
Dim command As New SqlCommand("Select * from [Table_login] where [Username] = @1 and [Password] = @2 ", connection)
connection.Open()
command.Parameters.AddWithValue("@1", UserTextBox.Text)
command.Parameters.AddWithValue("@2", PasswordTextBox.Text)
Using dr As SqlDataReader = command.ExecuteReader
Dim userFound As Boolean = False
Dim Name As String = ""
While dr.Read
userFound = True
Name = dr("Username").ToString
End While
If userFound = True Then
Form1.Show()
ElseIf userFound = False Then
MsgBox("Username or password is wrong!!!!!! ")
End If
End Using
connection.Close()
If Permissions = "user1" Then
Form1.RaportetToolStripMenuItem.Visible = False
End If
End Sub
结束班