.net级联删除自联表树结构

今天同事问我说如何删除树结构的自联表,要求是子树是无限的未知的。于是乎简单的方法实现了下。改天再优化过。如果有好的意见请各位看官给个意见,小的将感激不尽。不废话上代码上图
主要方法:(c# java的方法类似,函数格式不一样而已)
Function 级联删除(ByVal str_表名 As String, ByVal str_主键字段 As String, ByVal str_父字段标记 As String, ByVal str_主键值 As String) As Boolean
            Dim idslip = 获取ID串(str_表名, str_主键字段, str_父字段标记, str_主键值)
            Dim sql = ""
            Dim isok As Boolean
            Dim idarry = idslip.Split(",")
            sql = "delete " + str_表名 + " Where 1>0 and  " + str_父字段标记 + "='" + str_主键值 + "' or " + str_主键字段 + "='" + str_主键值 + "'"
            If idslip.Length > 0 Then
                For i = 0 To idarry.Length - 1
                    System.Web.HttpContext.Current.Response.Write("当前串是:" + idslip + "主键是:" + str_主键值 + "<br>")
                    isok = delete(sql)
                    级联删除(str_表名, str_主键字段, str_父字段标记, idarry(i))

                Next
                Return isok
            End If


            Return True
        End Function

        '获取某个记录下的所有下级记录ID
        Function 获取ID串(ByVal str_表名 As String, ByVal str_主键字段 As String, ByVal str_父字段标记 As String, ByVal str_主键值 As String) As String
            Dim DataConn As New SqlConnection(strConn)
            Dim strSQL As String
            Dim strIDslipt = ""
            strSQL = "SELECT   " + str_主键字段 + " FROM " + str_表名 + " Where 1>0 and  " + str_父字段标记 + "='" + str_主键值 + "'"

            Dim DataAdapter As New SqlDataAdapter(strSQL, DataConn)
            Dim DataSet As New DataSet
            Try
                DataSet.Clear()
                DataAdapter.Fill(DataSet)
                If DataSet.Tables(0).Rows.Count <> 0 Then
                    For i = 0 To DataSet.Tables(0).Rows.Count - 1
                        strIDslipt &= DataSet.Tables(0).Rows(i)(str_主键字段) + ","
                    Next
                End If
            Catch err As Exception
                Return ""
            Finally
                DataConn.Close()
            End Try
            DataSet.Dispose()
            Return strIDslipt
        End Function

        '删除方法
        Function delete(ByVal sql As String) As Boolean
            System.Web.HttpContext.Current.Response.Write(sql + "<br>")
            Dim sqlconn As New SqlConnection(strConn)
            Try
                Dim cmdTable As SqlCommand = New SqlCommand(sql, sqlconn)
                cmdTable.CommandType = CommandType.Text
                sqlconn.Open()
                cmdTable.ExecuteNonQuery()
                Return True
            Catch
                Return False
            Finally
                sqlconn.Close()
            End Try
        End Function


附件是表的数据和结构

测试页面的代码是:
Public Class index1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Btn_delete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_delete.Click
        Dim obj = New AutoCommon.数据操作()
        obj.级联删除("字典_行政区划表", "区划代码", "父级代码", TxB_值.Text.Trim)
       
    End Sub
End Class

你可能感兴趣的:(.net)