【发布时间】: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