我正在尝试建立与MySQL服务器的连接,从文本框中获取输入,使用该信息运行查询并将其存储在列表框中。
为了更进一步,我希望在文本框中键入时更新列表框。
每当我跑步时,vb都没有填充。我甚至尝试将查询更改为基本的,从securePasswordList.users中选择*;我无法得到任何东西。
有任何建议或批评吗?谢谢!还在学习VB相当多的东西......还有很长的路要走。
Imports MySql.Data.MySqlClient
Public Class Reveal
Public Property MyDataClass As SecurePasswordList
Dim lastNameInput As String
Dim firstNameInput As String
Dim connStr As String = "server=0.0.0.0;user=johndoe;database=securePasswordList;port=3306;password=password;"
Dim conn As New MySqlConnection(connStr)
Dim SQL As String = "select password from securePasswordList.users where LAST_NAME like ' " & lastNameInput & "%'"
Dim cmd As MySqlCommand = New MySqlCommand(SQL, conn)
Private Sub Reveal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub firstName_TextChanged(sender As Object, e As EventArgs) Handles lastNameTextBox.TextChanged
lastNameInput = lastNameTextBox.Text
listBox.Items.Add(cmd)
End Sub
Private Sub lastName_TextChanged(sender As Object, e As EventArgs) Handles firstNameTextBox.TextChanged
firstNameInput = firstNameTextBox.Text
End Sub
Private Sub listBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listBox.SelectedIndexChanged
End Sub
End Class
答案 0 :(得分:0)
您的一个问题是您实际上并未在任何地方执行MySqlCommand。另外,我想指出this link,因为在构建select-string的方式中,当前代码中存在潜在的安全问题。
以下是如何检索密码的示例:
Private Function RetrievePassword(ByVal lastName As String) As String
Using conn As New MySqlConnection(connStr)
Using comm As New MySqlCommand(query, conn)
comm.Parameters.AddWithValue("@LastName", lastName & "%'")
Dim result As Object = comm.ExecuteScalar()
If result Is Nothing Then
handle as you like...
End If
Return result.ToString
End Using
End Using
End Function
查询将包含参数的占位符,如下所示:
Private Const query As String =
"select password from securePasswordList.users where LAST_NAME like @LastName"