此代码有什么问题?我无法从数据库中检索数据

时间:2019-01-16 09:33:55

标签: vb.net phpmyadmin

我只是数据库基础的初学者,使用完此工具后,它仅显示msgbox并显示未知命令。

Try
    connectionDB.Open()
    cmdDB.CommandText = "SELECT * FROM rescuers WHERE LastName='" & searchres.Text & "';"
    readDB = cmdDB.ExecuteReader()
    With readDB
        .Read()
        rescid.Text = .Item("RescuerID")
        lname.Text = .Item("LastName")
        fname.Text = .Item("FirstName")



        .Close()
    End With

2 个答案:

答案 0 :(得分:0)

尝试像这样设置sqlcommand和sqldatareader:

    Dim command As New SqlCommand("SELECT * FROM rescuers WHERE LastName='" & searchres.Text & "'", connection)
    Dim reader As SqlDataReader = command.ExecuteReader()

也发布您的代码以设置SqlConnection。

答案 1 :(得分:0)

尝试使用参数避免SQL注入

cmdDB = New SqlCommand("SELECT * FROM rescuers WHERE LastName = @LastName", DatabaseConnection)

cmdDB.Parameters.AddWithValue("@LastName", searchres.Text)

readDB = cmdDB.ExecuteReader()

这是假设您已经按照以下方式定义了您的阅读器:

Dim readDB As SqlDataReader