VB MySql AddWithValue未成功传递该值

时间:2012-01-18 08:27:14

标签: mysql vb.net

我有以下VB代码

Dim cmd As New MySqlCommand("SELECT code FROM decoder WHERE ann_id = @aid", conn) 

cmd.Parameters.AddWithValue("@aid", 1)

Dim reader As MySqlDataReader = cmd.ExecuteReader()

如果我用

代替代码,那么reader.Read()则不会给我任何东西
Dim cmd As New MySqlCommand("SELECT code FROM decoder WHERE ann_id = 1", conn) 

Dim reader As MySqlDataReader = cmd.ExecuteReader()

reader.Read()给出正确的结果。尝试使用添加,但没有帮助。我在这里缺少什么?

非常感谢,

2 个答案:

答案 0 :(得分:0)

删除@

Dim cmd As New MySqlCommand("SELECT code FROM decoder WHERE ann_id = @aid", conn) 
cmd.Parameters.AddWithValue("aid", 1)
Dim reader As MySqlDataReader = cmd.ExecuteReader()

答案 1 :(得分:0)

Private Sub ButtonInsert_Click(sender As Object, e As EventArgs) Handles ButtonInsert.Click
    Dim conn As New MySqlConnection
    Dim cmd As New MySqlCommand

    conn.ConnectionString = Me.Tag.ToString

    Try
        conn.Open()
        cmd.Connection = conn

        cmd.CommandText = "INSERT INTO myTable VALUES(NULL, @number, @text)"
        cmd.Prepare()

        cmd.Parameters.AddWithValue("@number", 1) 'sets @number to be numeric
        cmd.Parameters.AddWithValue("@text", "One") 'sets @text to be characters

        For i = 1 To 1000

            cmd.Parameters("@number").Value = i
            cmd.Parameters("@text").Value = "A string value"

            cmd.ExecuteNonQuery()
        Next
    Catch ex As MySqlException
        MessageBox.Show("Error " & ex.Number & " has occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try