从数据库中删除记录时出错



我需要一些从数据库中删除记录的帮助。

我正试图从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

DataRow.Delete不会从数据库中删除此行。它将DataRowRowState标记为已删除。如果您调用Update,这将从DataAdapter进行检查。如果它的状态是Deleted,它将根据您必须提供的DeleteCommand查找它。

因此,您需要为DataAdapter提供一个DeleteCommand,它是从数据库中删除行的sql。

您想要删除它

_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"))

来源:从数据集和sql server 中删除记录

您将该表称为_DataSet.Tables(0),然后稍后将其称为_DataSet.Tables("0")。我打赌两者中的一个不正确。

相关内容

  • 没有找到相关文章

最新更新