vb.net sql语句不从access数据库返回任何行

cotxawn7  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(285)

我有以下登录winform的代码。当我连接到数据库并发出select语句时,没有返回任何行。我得到消息“没有行/列的数据。”
但是数据库中有行和列。
有人能告诉我我做错了什么吗?

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    Dim connectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("MySqlConnectionString").ConnectionString
    Using con As New OleDbConnection(connectionString)
        Dim intResult As Integer = 0
        ' MsgBox(connectionString)
        Try
            con.Open()

            Using cmd As New OleDbCommand("SELECT Gebruikersnaam FROM Gebruikers WHERE Gebruikers.Gebruikersnaam = @Username", con)
                cmd.Parameters.AddWithValue("@Username", UsernameTextBox.Text)
                cmd.Parameters.AddWithValue("@Password", PasswordTextBox.Text)
                Using dr As OleDbDataReader = cmd.ExecuteReader()

                    'intResult = CInt(cmd.ExecuteScalar)

                    'If intResult > 0 Then
                    MsgBox(dr.Item("Gebruikersnaam").ToString)
                    'End If

                    With dr
                        While .Read()
                            MsgBox(.HasRows)
                            'MsgBox(.Item("Gebruikersnaam"))
                            'TextBox1.Text = .Item("Gebruikersnaam") & vbCrLf
                        End While
                    End With
                End Using
            End Using

        Catch ex As Exception
            MsgBox(ex.Message)

            con.Close()
        End Try

        Me.Close()
    End Using
End Sub
oxosxuxt

oxosxuxt1#

问题在于检查 dr.Item() 在打电话之前 dr.Read() . 除此之外,请确保 UsernameTextBox 如果你真的存在于数据库中,修复那些讨厌的纯文本密码,你就没事了。

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    Dim connectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("MySqlConnectionString").ConnectionString
    Try
        Dim result As New StringBuilder()
        Using con As New OleDbConnection(connectionString)
        Using cmd As New OleDbCommand("SELECT Gebruikersnaam FROM Gebruikers WHERE Gebruikersnaam = @Username", con)

            cmd.Parameters.AddWithValue("@Username", UsernameTextBox.Text)
            cmd.Parameters.AddWithValue("@Password", PasswordTextBox.Text)
            con.Open()

             Using dr As OleDbDataReader = cmd.ExecuteReader()
                  While dr.Read() 
                      'MsgBox(dr("Gebruikersnaam"))
                      result.AppendLine(dr("Gebruikersnaam"))
                  End While
              End Using
        End Using
        End Using
        TextBox1.Text = result.ToString()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

最后一个问题。我不确定您使用的是哪个ole提供程序,但上次我检查了大多数提供程序,您希望在哪些地方使用ole是合理的 ? 占位符而不是命名参数。因此sql命令如下所示:

SELECT Gebruikersnaam FROM Gebruikers WHERE Gebruikersnaam = ?

但是,如果您真的使用mysql,正如连接字符串名称所暗示的那样,那么使用真正的mysql ado.net库而不是oledb确实会好得多:性能提升很小,错误消息传递更好,等等。

相关问题