【问题标题】:VB.NET SQL statement doesn't return any rows form Access databaseVB.NET SQL 语句不从 Access 数据库返回任何行
【发布时间】:2020-09-23 21:38:12
【问题描述】:

我有以下用于登录 winform 的代码。当我与数据库建立连接并进行选择语句时,我没有返回任何行。我收到消息“行/列不存在数据。”

但是数据库中有行有列。

谁能告诉我我做错了什么?

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

【问题讨论】:

  • 这能回答你的问题吗? OleDbParameters and Parameter Names
  • 啊,好旧的纯文本密码。我们又见面了。
  • 你不能在dr.Read()它之前调用MsgBox(dr.Item("Gebruikersnaam").ToString)
  • @nurchi,我很抱歉。没看到笑的情绪:)
  • 再看我的评论。您的光标不在一行上。

标签: sql vb.net winforms ms-access


【解决方案1】:

问题在于检查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:性能提升小,错误更好消息等。

【讨论】:

  • 密码字段是一个测试。首先,我想找出为什么我没有得到任何结果。感谢您的解决方案。
  • @Joel 因此,OleDbParameter 对象添加到 OleDbParameterCollection 的顺序必须直接对应于命令文本中参数的问号占位符的位置。 (来源docs.microsoft.com/en-us/dotnet/api/…
猜你喜欢
  • 2011-05-15
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
  • 2011-04-20
相关资源
最近更新 更多