为什么我的代码只读取数据库的第一行?

时间:2014-04-08 08:57:31

标签: vb.net

Dim con As New SqlConnection
con.ConnectionString = "Data Source=(local);Integrated Security=True"
Dim cmd As New SqlCommand("SELECT * FROM login where Admin = @Admin AND Password = @Password ", con)

'Set up parameters for the sqlcommand
cmd.Parameters.AddWithValue("@Admin", comb.Text)
cmd.Parameters.AddWithValue("@Password", Txtpass.Text)

'If the username or password is empty then throw an error
If comb.Text = String.Empty Then
        MessageBox.Show("Please choose the username.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf Txtpass.Text = String.Empty Then
        MessageBox.Show("Please enter the password.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
        Try
            'Open the connection and declare an sqlreader
            con.Open()
            Dim reader As SqlDataReader = cmd.ExecuteReader

            'If our reader has one or more rows then read those rows and compare the text

            If reader.HasRows = True Then
                reader.Read()
                Dim ReadUserName As String
                Dim ReadUserID As String
                ReadUserID = reader(3) 'This is the first field returned, most likely UserID
                ReadUserName = reader(1) 'This is the second field returned

                'If the username and password match then it's a success
                If comb.Text = reader("Admin").ToString And Txtpass.Text = reader.Item("Password").ToString Then

                    MessageBox.Show("Login successful" & ReadUserName)

                Else
                    'If they don't match than throw an error
                    MessageBox.Show("Login Failed." & Environment.NewLine & _
                                    "UserName and Password are casesensitive.", _
                                    Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            con.Close()
        End Try
    End If

2 个答案:

答案 0 :(得分:1)

告诉您的代码阅读一行

If reader.HasRows = True Then
   reader.Read()

如果您想从SqlDataReader读取所有行,则需要使用循环(此处为C#):

while (reader.Read())
{
    ..... (read the individual values for the current row) ......
} 

.Read()调用将返回true,只要从SqlDataReader读取了一行 - 现在从行中获取数据,存储或处理它 - 无论你是什么需要做 - 然后你的下一次调用reader.Read()检查是否可以读取另一行。如果是这样:重复处理。

如果reader.Read()返回false,那么所有行都已被读取并且您已完成。

答案 1 :(得分:1)

如果命令只运行一次代码并读取一行。所以使用while循环来读取所有行

       If reader.HasRows = True Then
           While reader.Read()
            Dim ReadUserName As String
            Dim ReadUserID As String
            ReadUserID = reader(3) 'This is the first field returned, most likely UserID
            ReadUserName = reader(1) 'This is the second field returned

            'If the username and password match then it's a success
            If comb.Text = reader("Admin").ToString And Txtpass.Text = reader.Item("Password").ToString Then

                MessageBox.Show("Login successful" & ReadUserName)

            Else
                'If they don't match than throw an error
                MessageBox.Show("Login Failed." & Environment.NewLine & _
                                "UserName and Password are casesensitive.", _
                                Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End While ' I don't know much of vb.net'
    End If