如何更新vb.net 2013 SQL Server 2008中的特定列

时间:2017-05-25 04:13:30

标签: sql-server vb.net textbox

我有4张桌子:Code_Product, Name_Product, Color_Product, Size_Product

代码:

 Public Sub ExecuteQuery(query As String)
       Dim command As New SqlCommand(query, connection)
      command.ExecuteNonQuery()
End Sub

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click

Dim updateQuery As String = "UPDATE tbl_Product SET Name_Product = '" & txtNameProduct.Text & "' , Color_Product = '" & txtColorProduct.Text & "', Size_Product = '" & txtSizeProduct.Text & "' where Kode_Produk= '" & txtKodeProduk.Text & "'"

ExecuteQuery(updateQuery)

MessageBox.Show("Data has been changed")

注意:代码有效。

但是,代码会更改所有列,我只想更改一列。怎么样?

1 个答案:

答案 0 :(得分:-1)

替换此行

Dim updateQuery As String = "UPDATE tbl_Product SET Name_Product = '" & txtNameProduct.Text & "' , Color_Product = '" & txtColorProduct.Text & "', Size_Product = '" & txtSizeProduct.Text & "' where Kode_Produk= '" & txtKodeProduk.Text & "'"

有了这个。仅传递您要更新的列。

Dim updateQuery As String = "UPDATE tbl_Product SET Name_Product = '" & txtNameProduct.Text & "'  where Kode_Produk= '" & txtKodeProduk.Text & "'"