0

我已经编写了下面的代码来更新我的表格,但它不起作用。

sqlstr = "UPDATE Student SET " & S & " = @field1, " & L & " = @field2, " & R & " =@field3, " & W & "=@field4 WHERE Code='" & StdID & "'"
DBCmd = New MySql.Data.MySqlClient.MySqlCommand(sqlstr, DBConn)
With DBCmd
        .Parameters.AddWithValue("@field1", CB_S.SelectedItem)
        .Parameters.AddWithValue("@field2", CB_L.SelectedItem)
        .Parameters.AddWithValue("@field3", CB_R.SelectedItem)
        .Parameters.AddWithValue("@field4", CB_W.SelectedItem)
    End With
    DBCmd.Dispose()

其中 S、L、R 和 W 是字符串:S1、L1、R1、W1。StdID 是一个整数。CB_S、CB_L、CB_R 和 CB_W 是组合框。

谁能告诉我出了什么问题?

4

1 回答 1

2

你没有打开connection和调用ExecuteNonQUery()。还参数化了值Code

sqlstr = "UPDATE Student SET " & S & " = @field1, " & L & " = @field2, " & R & " =@field3, " & W & "=@field4 WHERE Code=@code"
DBCmd = New MySql.Data.MySqlClient.MySqlCommand(sqlstr, DBConn)
With DBCmd
        .Parameters.AddWithValue("@field1", CB_S.SelectedItem)
        .Parameters.AddWithValue("@field2", CB_L.SelectedItem)
        .Parameters.AddWithValue("@field3", CB_R.SelectedItem)
        .Parameters.AddWithValue("@field4", CB_W.SelectedItem)
        .Parameters.AddWithValue("@code", StdID)
End With
DBConn.Open()
DBCmd.ExecuteNonQuery()
于 2013-04-07T17:14:27.410 回答