Private Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click
mysqlconn.Open()
cmd.CommandText = "select * from login "
cmd.Connection = mysqlconn
dr = cmd.ExecuteReader
dr.Read()
If txtuser.Text = "" And txt_password.Text = "" Then
MsgBox("Please enter user name and password", MsgBoxStyle.Information, "Login form")
Else
If txtuser.Text = "" Then
MsgBox("Please select user name", MsgBoxStyle.Information, "Login form")
Else
If txt_password.Text = "" Then
MsgBox("Please enter password", MsgBoxStyle.Information, "Login form")
'If (dr.FieldCount > 0) Then
'Dim i As Integer = dr(1)
' For i = 1 To dr.FieldCount
'user = dr("user").ToString
' password = dr("password").ToString
ElseIf ((txtuser.Text = '" & txt_username.text & "') And (txt_password.Text = '" & txt_password.text & "')) Then
form2.Show()
Else : MsgBox("Password missmatch.", vbCritical, "Invalid password")
End If
End If
End If
dr.Close()
cmd.Dispose()
mysqlconn.Close()
mysqlconn.Dispose()
End Sub
我有一个表login.username和密码是两列。我试图从上面的代码中的登录表中获取应用程序中的用户名和密码,但我无法从登录表中获取第二行条目,因为我有两个rowrs( 1)username = abc,passowrd = abc 2)username = xyz,password = xyz) 如何在运行时获得第二次登录? 感谢
答案 0 :(得分:0)
您可以使用DataTable
来保留所有行,也可以使用DataReader.Read
将读者推进到下一条记录。
DataTable
Dim tblUsers = New DataTable
Using con = New MySql.Data.MySqlClient.MySqlConnection(conString)
Using da = New MySql.Data.MySqlClient.MySqlDataAdapter("select * from login", con)
da.Fill(tblUsers)
End Using
End Using
If tblUsers.Rows.Count > 1 Then
Dim user = tblUsers.Rows(1).Field(Of String)("User")
Dim password = tblUsers.Rows(1).Field(Of String)("Password")
End If
的DataReader:
Using con = New MySql.Data.MySqlClient.MySqlConnection(conString)
Using cmd = New MySql.Data.MySqlClient.MySqlCommand("select * from login", con)
Using reader = cmd.ExecuteReader()
If reader.Read Then
' first user '
If reader.Read Then
' second user '
Dim user = reader.GetString("User")
Dim password = reader.GetString("Password")
End If
End If
End Using
End Using
End Using
答案 1 :(得分:0)
如果您的目的是检查这对夫妇(用户名,密码),那么,请在表格中搜索这对夫妇,而不是在DataReader中搜索。这里唯一的假设是这对夫妇(用户名,密码)在登录表中是唯一的。
在这种情况下,请在查询中使用两个参数:
cmd.CommandText = "SELECT * FROM login WHERE user = @username AND password = @password"
cmd.Parameters.Add(new SqlParameter("username", username))
cmd.Parameters.Add(new SqlParameter("password", password))
'then execute and check the results:
dr = cmd.ExecuteReader()
if dr.Read() then
'login successful
else
'login failed
end