VB.NET不会从SQL DB中删除记录

时间:2014-02-02 10:41:42

标签: sql vb.net

我正在尝试编写的应用程序中存在一些相当恼人的问题。

我有一个带有小表(“主”)的数据库,我在创建表时手动输入数据。我可以删除我手动输入的3行中的一行,没问题,但如果我从应用程序添加一行它不会保存到数据库,我无法删除它。 此外,我编辑的所有值都没有保存到数据库。我怀疑这两个问题是相关的,希望你能帮助我弄清楚出了什么问题。

这是“删除”按钮的代码:

Private Sub btnSearchDelete_Click(sender As Object, e As EventArgs) Handles btnSearchDelete.Click

    Try

        Me.MainTableAdapter.Delete(searchIP.Text, searchSubnet.Text, searchVLAN.Text, searchAlive.Checked, searchReserved.Checked, searchUser.Text, searchDate.Text, searchFail.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 = "Deleted record."
            sysmsg.ForeColor = Color.Red
            Application.DoEvents()
        Loop Until DateDiff(DateInterval.Second, tmCurTime, Date.Now) = wait
        sysmsg.Text = ""

    Catch ex As Exception

        MsgBox("Delete failed!" - ex.Message)

    End Try
    Ds.AcceptChanges()
    Me.MainTableAdapter.Update(Me.Ds.main)

    'clears search filter
    Me.MainBindingSource.Filter = Nothing
    Me.MainTableAdapter.Fill(Me.Ds.main)

    grid.Refresh()

End Sub

(网格指的是主应用页面上的datagridview)

我意识到我的代码很可能,嗯,非常凌乱 - 我是超级新事物的开发方面,这只是我迄今为止所做的第三个应用程序。

您可以给予任何帮助。

:: EDIT :: 好的,这就是MainTableAdapter.Delete()在SQL语言中调用的内容: DELETE FROM [dbo].[main] WHERE (([IP] = @Original_IP) AND ((@IsNull_SUBNET = 1 AND [SUBNET] IS NULL) OR ([SUBNET] = @Original_SUBNET)) AND ((@IsNull_VLAN = 1 AND [VLAN] IS NULL) OR ([VLAN] = @Original_VLAN)) AND ((@IsNull_ALIVE = 1 AND [ALIVE] IS NULL) OR ([ALIVE] = @Original_ALIVE)) AND ((@IsNull_RESERVED = 1 AND [RESERVED] IS NULL) OR ([RESERVED] = @Original_RESERVED)) AND ((@IsNull_USR = 1 AND [USR] IS NULL) OR ([USR] = @Original_USR)) AND ((@IsNull_DATE = 1 AND [DATE] IS NULL) OR ([DATE] = @Original_DATE)) AND ((@IsNull_FAIL = 1 AND [FAIL] IS NULL) OR ([FAIL] = @Original_FAIL)))

0 个答案:

没有答案