我使用数据网格视图来显示数据库中的信息,我无法正确更新它.我可以看到数据库中的数据,但是当我更改信息并保存它时,它会更改整个列的信息.我的行需要在同一列中具有不同的值,因此这是一个问题.
- 到目前为止,这是我的代码,程序所做的是列出输入的事务.
Public Class Form1 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click If MsgBox("Add new transaction?", MsgBoxStyle.YesNo, " ") <> 7 Then If IsNumeric(in_cost.Text) Then Dim x As Int16 = Database1DataSet.Table1.Count + 1 '//gets # value Me.Table1TableAdapter.Insert(Format(Now, "dddd, M/dd/yy"), x, in_cost.Text, in_prod.Text, in_type.Text, in_note.Text, 9999.99) '//Writes new index to DB table Me.Table1TableAdapter.Fill(Me.Database1DataSet.Table1) Else MsgBox("Cost is non-numeric!") End If End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Table1TableAdapter.Fill(Me.Database1DataSet.Table1) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Table1TableAdapter.Fill(Me.Database1DataSet.Table1) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Table1TableAdapter.Update(Me.Database1DataSet.Table1) End Sub End Class
-SQL UPDATE命令:
UPDATE Table1 SET Balance = @p1, Note = @p2, Type = @p3, Product = @p4, Cost = @p5, # = @p6, Date = @p7
在此先感谢您的帮助.
它会更改每一行,因为update语句没有where子句.如果没有where子句,则没有谓词,因此它匹配每一行,因此每行都会更新.
您需要在update语句中使用where子句(谓词).在更新语句的末尾可能是"where id = @id".