2017-05-09 46 views
0

我嘗試添加parameters.addwithvalue。 改變之前的代碼就是這樣..........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

不要將MySqlConnection和MySqlCommand存儲爲類中的字段,請不要重複使用它們。這只是錯誤的根源,沒有任何好處。創建,初始化,使用和處理('使用'語句),無論你需要他們。 –

+0

只要你的表單已經打開並且你改變了索引值就可以創建值所以要麼在子表單內使用一個命令,要麼在表單加載時用'.Add'初始化參數,然後在索引上改變'.Value'更改 – Mederic

回答

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 
2

不要儲存MySqlConnectionMySqlCommand在你的類中的字段,不要重複它們。這只是錯誤的根源,沒有任何好處。創建,初始化,使用和處理(Using -statement)他們無論你需要他們,所以在這種方法。

你不清除的參數,這就是爲什麼你會得到第二次使用此錯誤。

因此,一個簡單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