1

我试图使用 LINQ 从数据库中删除一条记录。我已经给系统提供了要删除的行键,它确实返回了一行。

这是我的代码:

 Public Function DeleteHydrantByKey(ByVal objDeptGUID As String, ByVal objLogonToken As String, ByVal objSesionToken As String, ByVal objHydrantKey As Integer) As String
    'Check if Token is good
    If CheckToken(objSesionToken, objDeptGUID) = False Then
        Return "Session Error"
    End If
    'Check if login Token is good
    If CheckLoginToken(objDeptGUID, objLogonToken) = False Then
        Return "Logon Session Error"
    End If

    Try
        Dim lbHydrants As New linqAlarmDumpDataContext

        'Check for Hydrans in system
        Dim GetHydrants = From r In lbHydrants.tbHydrants _
                          Where r.key = objHydrantKey
                          Select r

        'Check For Hydrent Data
        If GetHydrants.Count = 0 Then
            Return "Not Found"
        End If


        'Deletes Hydrent Data After Check was ok
        lbHydrants.tbHydrants.DeleteOnSubmit(GetHydrants)
        lbHydrants.SubmitChanges()



        Return "OK"
    Catch ex As Exception
        Return "Error"
    End Try
End Function
4

1 回答 1

0
    Public Function DeleteHydrantByKey(ByVal objDeptGUID As String, ByVal objLogonToken As String, ByVal objSesionToken As String, ByVal objHydrantKey As Integer) As String
    'Check if Token is good
    If CheckToken(objSesionToken, objDeptGUID) = False Then
        Return "Session Error"
    End If
    'Check if login Token is good
    If CheckLoginToken(objDeptGUID, objLogonToken) = False Then
        Return "Logon Session Error"
    End If

    Try
        Dim lqHydrants As New linqAlarmDumpDataContext

        'Check for Hydrans in system
        Dim GetHydrants = From r In lqHydrants.tbHydrants _
                          Where r.key = objHydrantKey
                          Select r

        'Check For Hydrent Data
        If GetHydrants.Count = 0 Then
            Return "Not Found"
        End If


        'Deletes Hydrent Data After Check was ok
        lqHydrants.tbHydrants.DeleteAllOnSubmit(GetHydrants)
        lqHydrants.SubmitChanges()



        Return "OK"
    Catch ex As Exception
        Return "Error"
    End Try
End Function
于 2012-08-15T21:49:18.327 回答