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() –