如何使用linq从数据库中删除记录?

时间:2012-08-15 21:22:36

标签: linq web-services linq-to-sql vb.net-2010

我试图使用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

1 个答案:

答案 0 :(得分: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