VB.net不使用SQL

时间:2014-03-31 18:52:30

标签: mysql vb.net easyphp

我有一个问题就是吃掉了我的大脑。 我有一个包含2个表单的项目:1从我的数据库中提取数据(名称和姓氏),另一个用于检查用户的用户输入是否正确(匹配名称和姓氏)。第1种形式的代码是: http://pastebin.com/rg5GMuu6

第二个代码粘贴在这里 我不知道如何修复此错误。我听说过某种适配器或某种东西......帮助 Ty提前

我正在使用MySQL(Easy PHP); 上传一些照片: error sql entries 1st form works properly

第一种形式没有任何问题,第二种形式给我这个错误

导入MySql.Data 导入MySql.Data.MySqlClient 公共类Form2

Dim dbCon As MySqlConnection
Dim strQuery As String = ""
Dim SQLCmd As MySqlCommand
Dim DR As MySqlDataReader


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Try

        'just a message

        MsgBox(" You are searching for the users: " & vbCrLf & "Name: " & TextBox1.Text & vbCrLf & "Surname: " & TextBox2.Text)


        ' connecting to the database

        dbCon = New MySqlConnection("Server = localhost; Database = users; Uid = root; Pwd = password")
        strQuery = "SELECT users.name, users.surname FROM users" & _
            " WHERE users.name = @Username AND users.surname = @UserPassword"

        SQLCmd = New MySqlCommand(strQuery, dbCon)
        SQLCmd.Parameters.AddWithValue("@Username ", TextBox1.Text)
        SQLCmd.Parameters.AddWithValue("@UserPassword", TextBox2.Text)

        'Database open


        dbCon.Open()
        DR = SQLCmd.ExecuteReader

        If DR.HasRows = 0 Then
            MsgBox("Not a match", MsgBoxStyle.Critical)
        Else
            MsgBox("You guessed the correct name: " & TextBox1.Text & "and the surname: " & TextBox2.Text)
        End If


        'Close
        DR.Close()
        dbCon.Close()


    Catch ex As Exception

        MsgBox("Failure to communicate " & vbCrLf & vbCrLf & ex.Message)

    End Try

End Sub

结束班

使用调试器捕获所有错误 enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

.AddWithValue语句似乎有些奇怪的情况。我发现使用以下两行代码设置参数值会更好。

cmd.Parameters.Add(New SqlParameter("@UserName", Data.SqlDbType.NVarChar)).Direction = ParameterDirection.Input
cmd.Parameters("@UserName").Value = textbox1.text'obviously don't need this if it is an Output Param