我需要一些帮助来删除数据库中的记录。
我正在尝试从SQL Server数据库中的表中删除记录。
我错过了什么吗?
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
_DataSet.Tables(0).Rows(CInt(txtCurrent.Text) - 1).Delete()
' tho, it can remove the deleted rows
' we cannot call the DataSet.AcceptChanges method
' because the DataAdapter would not recognize the delete row
' by the time DataAdapter.Update(DataSet) is called.
EnableNavigation()
cmdSave.Enabled = True ' let user update the underlying database
' after deleting the current record, the current record still points to the
' deleted record (though it cannot be updated).
' The user must MoveNext/Back to view other records.
End Sub
答案 0 :(得分:3)
DataRow.Delete
不会从数据库中删除此行。它将DataRow
' s RowState
标记为deleted。如果您致电Update
,我们会从DataAdapter
检查此项。如果状态为Deleted
,则会根据您必须提供的DeleteCommand
查找其状态。
因此,您需要为DataAdapter
提供DeleteCommand
,这是从数据库中删除该行的SQL。
答案 1 :(得分:0)
您想删除它
_DataSet.Tables(0).Rows(CInt(txtCurrent.Text) - 1).Delete()
Dim adapter As New SqlDataAdapter
Dim cmdBuilder As New SqlCommandBuilder(adapter)
Dim DutyDetails As String = "SELECT * from MyTable"
adapter.SelectCommand = New SqlCommand(DutyDetails, SQLConn)
adapter.UpdateCommand = cmdBuilder.GetUpdateCommand
adapter.DeleteCommand = cmdBuilder.GetDeleteCommand
Dim cb As SqlCommandBuilder = New SqlCommandBuilder(adapter)
adapter.Update(_DataSet.Tables("0"))
答案 2 :(得分:0)
您将表格称为_DataSet.Tables(0)
,然后稍后将其称为_DataSet.Tables("0")
我打赌其中一个不正确。