如何更新SQL数据库中的特定条目

时间:2017-06-09 06:20:22

标签: sql vb.net visual-studio-2015

Private Sub cmdRRupdate_Click(sender As Object, e As EventArgs) Handles cmdRRupdate.Click
        Dim i As Integer
        Dim o As Integer
        i = DataGridView1.CurrentRow.Index
        o = DataGridView1.CurrentCell.ColumnIndex

        cn.Open()
        cmd.CommandText = "update RepairRenovation Set Project_ID = '" & txtRRprojectID.Text & "', Date = '" & rrIdate.Value & "', Source_of_Fund = '" & txtRRsof.Text & "', Contractor = '" & txtRRcontractor.Text & "', Scope_of_Works = '" & txtRRsow.Text & "' where convert (varchar, ID_Number) = '" & DataGridView4.Item(0, i).Value & "'"
        cmd.ExecuteNonQuery()
        cn.Close()
        txtRRprojectID.Text = ""
        txtRRsof.Text = ""
        txtRRcontractor.Text = ""
        txtRRsof.Text = ""
        txtRRsow.Text = ""
        ref3()
    End Sub

我使用where convert (varchar, Project_ID)但每当我更新一个条目时,它都会更新所有具有相同Project_ID的人的条目。因此,我使用ID_Number作为Primary Key来更新特定条目。但每当我尝试更新它时,该条目都不会更改/更新。

1 个答案:

答案 0 :(得分:0)

试试这个:

cmd.CommandText = "update RepairRenovation Set Project_ID = '" & txtRRprojectID.Text & "', Date = '" & rrIdate.Value & "', Source_of_Fund = '" & txtRRsof.Text & "', Contractor = '" & txtRRcontractor.Text & "', Scope_of_Works = '" & txtRRsow.Text & "' where ID_Number = " & DataGridView4.Item(0, i).Value

您是否调试了代码?你看过那个字符串里面的命令文本是什么吗?