0
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

我有一个表登录。用户名和密码是两列。我试图从上面代码中的登录表中获取应用程序中的用户名和密码,但我无法从登录表中获取第二行条目,因为我有两个 rowrs(1) 用户名=abc,passowrd=abc 2) 用户名​​=xyz,密码=xyz) 我如何在运行时获得第二个登录条目?谢谢

4

2 回答 2

0

If your aim is to check the couple (username, password), well, search the couple in the table and not in the DataReader. The only assumption here is that the couple (username, password) is unique in the login table.

In this case, use two parameters in the query:

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
于 2012-07-12T08:20:09.873 回答
0

您可以使用 aDataTable来保存所有行,也可以使用DataReader.Read将阅读器推进到下一条记录。

  1. 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
    
  2. 数据读取器:

    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
    
于 2012-07-12T08:15:54.110 回答