0
我试图用GetDeleteCommand()和DataAdapter.Update()删除数据库中的一行。使用GetInsertCommand()和GetUpdateCommand()方法处理所有事务,但GetDeleteCommand()失败,我的意思是,“运行”到该指令上,但数据仍然保留在表中。getdeletecommand不会删除任何东西
这里是我的代码:
Public Function delete(ByVal tabla As String, ByVal arg As VariantType) As Boolean
Dim ok As Boolean = False
Dim dataSet As DataSet = New DataSet(tabla)
Dim dataRow As DataRow
Dim dataTable As DataTable
Me.sqlComm.Connection = Me.sqlConn
Me.sqlComm.CommandText = "Select * from " & tabla
Try
Me.sqlDA.SelectCommand = Me.sqlComm
'llenar el dataset con los datos de la consulta
Me.sqlDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
Me.sqlDA.Fill(dataSet, tabla)
'nuevo commandbuilder
Me.sqlCB = New SqlCommandBuilder(Me.sqlDA)
'crear el buffer de la tabla
dataTable = dataSet.Tables(tabla)
'buscar el valor en la tabla
dataRow = dataTable.Rows.Find(arg)
'obtener el delete command
Me.sqlDA.DeleteCommand = Me.sqlCB.GetDeleteCommand(True)
'actualizar los registros.
'se supone q con el DeleteCommand deberia borrar en este punto el dato
Me.sqlDA.Update(dataSet, tabla)
ok = True
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString() & vbCrLf & ex.Message())
Me.err = ex.StackTrace.ToString() & vbCrLf & ex.Message()
End Try
Me.sqlConn.Close()
Return ok
End Function
如前面所说的,没有错误的所有代码运行,但找到的数据和从我的代码“已删除”仍保留在表中。
有什么不对?或者如何做正确的命令或者如何做到这一点?
谢谢
是的......没错,但是随后使用sqlcommand.getdeletecommand并不需要使用datarow.delete() –