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

时间:2022-05-02 15:41:30

I need some help with delete record from database.

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

I am trying to delete a record from a table in my SQL Server database.

我试图从我的SQL Server数据库中的表中删除记录。

Am I missing something?

我错过了什么吗?

  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

3 个解决方案

#1


3  

DataRow.Delete does not delete this row from database. It marks the DataRow's RowState as deleted. This will be checked from a DataAdapter if you call Update. If it's state is Deleted it will look for its according DeleteCommand which you have to provide.

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

So you need to provide a DeleteCommand for your DataAdapter which is the sql to delete the row from the database.

因此,您需要为DataAdapter提供DeleteCommand,这是从数据库中删除行的SQL。

#2


0  

You want to Delete it

你想删除它

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

Source: Deleting a record from dataset and sql server

来源:从数据集和SQL Server中删除记录

#3


0  

You refer to the table as _DataSet.Tables(0), then refer to it later as _DataSet.Tables("0") I bet one of the two is incorrect.

你将表称为_DataSet.Tables(0),然后将其称为_DataSet.Tables(“0”)我打赌其中一个是不正确的。

#1


3  

DataRow.Delete does not delete this row from database. It marks the DataRow's RowState as deleted. This will be checked from a DataAdapter if you call Update. If it's state is Deleted it will look for its according DeleteCommand which you have to provide.

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

So you need to provide a DeleteCommand for your DataAdapter which is the sql to delete the row from the database.

因此,您需要为DataAdapter提供DeleteCommand,这是从数据库中删除行的SQL。

#2


0  

You want to Delete it

你想删除它

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

Source: Deleting a record from dataset and sql server

来源:从数据集和SQL Server中删除记录

#3


0  

You refer to the table as _DataSet.Tables(0), then refer to it later as _DataSet.Tables("0") I bet one of the two is incorrect.

你将表称为_DataSet.Tables(0),然后将其称为_DataSet.Tables(“0”)我打赌其中一个是不正确的。