Parameters.AddWithValue:参数已经定义

时间:2017-05-09 08:09:43

标签: vb.net parameters combobox

我尝试添加一个parameters.addwithvalue。 在更改之前,代码就像..........

    Private Sub ComboBox7_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox7.SelectedIndexChanged


    Me.Cursor = Cursors.WaitCursor
    MysqlConn.Close()
    MysqlConn.Open()
    COMMAND.CommandText = "select logo from licenses where name = '" & ComboBox7.Text & "'"
    COMMAND.Connection = MysqlConn

    Dim da As New MySqlDataAdapter(COMMAND)
    Dim ds As New DataSet()
    da.Fill(ds, "projectimages")
    Dim c As Integer = ds.Tables(0).Rows.Count
    If c > 0 Then
        If IsDBNull(ds.Tables(0).Rows(c - 1)("logo")) = True Then
            PictureBox6.Image = Nothing
        Else
            Dim bytBLOBData() As Byte = ds.Tables(0).Rows(c - 1)("logo")
            Dim stmBLOBData As New MemoryStream(bytBLOBData)
            PictureBox6.Image = Image.FromStream(stmBLOBData)
        End If
    End If
    Me.Cursor = Cursors.Default
End Sub

现在我尝试这样做,无需成功添加paramatrers.addwithValue:

    Private Sub ComboBox7_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox7.SelectedIndexChanged


    Me.Cursor = Cursors.WaitCursor
    MysqlConn.Close()
    MysqlConn.Open()
    'COMMAND.CommandText = "select logo from licenses where name = '" & ComboBox7.Text & "'"
    COMMAND.CommandText = "select logo from licenses where name = @ComboBox7Select"
    COMMAND.Parameters.AddWithValue("@ComboBox7Select", If(String.IsNullOrEmpty(ComboBox7.Text), DBNull.Value, ComboBox7.Text))
    COMMAND.Connection = MysqlConn

    Dim da As New MySqlDataAdapter(COMMAND)
    Dim ds As New DataSet()
    da.Fill(ds, "projectimages")
    Dim c As Integer = ds.Tables(0).Rows.Count
    If c > 0 Then
        If IsDBNull(ds.Tables(0).Rows(c - 1)("logo")) = True Then
            PictureBox6.Image = Nothing
        Else
            Dim bytBLOBData() As Byte = ds.Tables(0).Rows(c - 1)("logo")
            Dim stmBLOBData As New MemoryStream(bytBLOBData)
            PictureBox6.Image = Image.FromStream(stmBLOBData)
        End If
    End If
    Me.Cursor = Cursors.Default
End Sub

错误“参数'@ ComboBox7Select'已经定义。”

我做什么改变工作?

谢谢你。

2 个答案:

答案 0 :(得分:2)

不要将MySqlConnectionMySqlCommand作为字段存储在您的课程中,根本不要重复使用它们。这只是一个没有任何好处的错误来源。在任何需要的地方创建,初始化,使用和处理(Using - 语句),所以在这种方法中。

您无法清除参数,这就是您在第二次使用时出现此错误的原因。

在添加之前,简单的COMMAND.Parameters.Clear()可以解决问题。但是使用我上面提到的方法:

Private Sub ComboBox7_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox7.SelectedIndexChanged
    Dim ds As New DataSet()
    Dim licenseNameValue As Object = DBNull.Value
    If Not String.IsNullOrEmpty(ComboBox7.Text) Then licenseNameValue = ComboBox7.Text

    Using mysqlConn As New MySqlConnection("ConnectionString...")
        Using da As New MySqlDataAdapter("select logo from licenses where name = @licenseName", mysqlConn)
            da.SelectCommand.CommandText = "select logo from licenses where name = @licenseName"
            da.SelectCommand.Parameters.AddWithValue("@licenseName", licenseNameValue)
            da.Fill(ds, "projectimages") ' you dont need to open/close the connection with DataAdapter.Fill
        End Using
    End Using

    ' ....
End Sub

答案 1 :(得分:1)

问题是您使用的是全局变量COMMAND,每次所选索引在组合中发生变化时都会使用该变量。您每次使用以下命令初始化命令:

COMMAND=New MySqlCommand()

或者您必须清除参数:

COMMAND.Parameters.Clear()
COMMAND.Parameters.AddWithValue("@ComboBox7Select", If(String.IsNullOrEmpty(ComboBox7.Text), DBNull.Value, ComboBox7.Text))

但最好的方法是始终使用MySql结构创建和处置Using个对象:

Using MysqlConn As New MySqlConnection(connString)
    Using COMMAND As New MySqlCommand()
    'your code 
    End Using
End Using