VB.NET tableadapter.update()不更新数据库

时间:2014-02-03 06:10:55

标签: sql vb.net

我正在尝试从VB.NET WF应用程序更新SQL DB中的记录,并且tableadapter.update()命令不起作用。

至少部分我遇到的问题是主键错误。出于某种原因,除了PK之外,它不会让我更新所有列值。

这是我的“保存”功能的代码,它应该根据表单上文本框中的值更新数据库记录。 正如你从下面的代码中看到的,我尝试了一些不同的更新方法,但似乎都没有。你们可以提供给我指向正确方向的任何帮助都会有所帮助。

Try
            If Ds.main.Rows.Count <> 0 Then
                'send data to DT for editing

                'rows to write
                'Ds.main.Rows(searchIP.Text)("IP") = searchIP.Text
                'Ds.main.Rows(searchIP.Text)("HOSTNAME") = searchHost.Text
                'Ds.main.Rows(searchIP.Text)("SUBNET") = searchSubnet.Text
                'Ds.main.Rows(searchIP.Text)("VLAN") = searchVLAN.Text
                'Ds.main.Rows(searchIP.Text)("ALIVE") = searchAlive.Checked
                'Ds.main.Rows(searchIP.Text)("RESERVED") = searchReserved.Checked
                'Ds.main.Rows(searchIP.Text)("USR") = searchUser.Text
                'Ds.main.Rows(searchIP.Text)("NOTES") = searchNotes.Text
                'Ds.main.Rows(searchIP.Text)("FAIL") = searchFail.Text
                'Ds.main.Rows(searchIP.Text)("date") = Date.Now

                Ds.Tables("main").Rows(0).Item("notes") = searchNotes.Text

                'writes changes to DB
                Ds.AcceptChanges()
                Me.MainTableAdapter.Update(Me.Ds.main)

                ' refreshes the table on the All Hosts tab
                Me.MainTableAdapter.Fill(Me.Ds.main)


                ' clears all fields on the Search tab
                boxSearch.Text = ""
                searchIP.Text = ""
                searchHost.Text = ""
                searchSubnet.Text = ""
                searchVLAN.Text = ""
                searchUser.Text = ""
                searchNotes.Text = ""
                searchAlive.Checked = False
                searchReserved.Checked = False
                searchFail.Text = ""
                searchDate.Text = ""



                ' updates system message bar for 1 second
                Dim tmCurTime As DateTime = Date.Now
                Dim wait As Int16 = 1  ' Time in seconds
                Do
                    sysmsg.Text = "Saved!"
                    sysmsg.ForeColor = Color.Green
                    Application.DoEvents()
                Loop Until DateDiff(DateInterval.Second, tmCurTime, Date.Now) = wait
                sysmsg.Text = ""
            End If
        Catch ex As Exception
            MsgBox("error" + ex.Message)
        End Try

1 个答案:

答案 0 :(得分:0)

我认为你可能需要删除这一行:

Ds.AcceptChanges()

了解此方法的更多详细信息 - http://msdn.microsoft.com/en-us/library/system.data.dataset.acceptchanges(v=vs.110).aspx