使用C#和SQL server根据父级查询该子级(包括父级)下所有对应的数据

使用C#根据父级查询对应的子级

前端通过选中树形下拉框进行查询,id可能为父级也可能为子级

public static string GetChildFieldIds(int id)
{
    IFieldBL fbl = new FieldBL();
    List<IntellManuSchool_Field> fieldAll = fbl.GetAllField();
    string result = id + ",";
    List<IntellManuSchool_Field> second = fieldAll.FindAll(s => s.ParentId == id && s.IsShow == 1).OrderBy(x => x.Orders).ToList();
    if (second != null && second.Count > 0)
    {
        foreach (IntellManuSchool_Field item in second)
        {
            result += item.FieldID + ",";
            List<IntellManuSchool_Field> three = fieldAll.FindAll(s => s.ParentId == item.FieldID && s.IsShow == 1).OrderBy(x => x.Orders).ToList();
            if (three != null && three.Count > 0)
            {
                foreach (IntellManuSchool_Field entity in three)
                {
                    result += entity.FieldID + ",";
                }
            }
        }
    }
    return result.TrimEnd(',');
}

最终的到的结果类似于"1,2,3,4,…"以逗号隔开的字符串id

通过SQL server通过对应的子级查询对应的数据

USE [base]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create procedure [dbo].[IntellManuSchool_CourseStatisticsByPage]
(  
	@Title varchar(20),
	@Field VARCHAR(100),--前端传来以逗号隔开的字符串id
	@page_size int,
	@current_page int,
	@total int output
) 
as
begin
DECLARE @theStr varchar(2000)
set @theStr='1=1 '
if @Title<>''
	set @theStr=@theStr+' and Title like ''%'+@Title+'%'''
if @Field<>''
begin
    DECLARE @theField varchar(1000)
	set @theField = ''
	declare @idx int
    declare @slice varchar(10)
    select @idx = 1         
	while @idx!= 0
    begin
        set @idx =charindex(',',@Field)
        if @idx!=0
            set @slice =left(@Field,@idx - 1)
        else
			set @slice = @Field
        if(len(@slice)>0)
		begin			
			if @theField = ''
				set @theField = 'charindex('',''+'''+@slice+'''+'','','',''+Field+'','') > 0'
		    else
				set @theField = @theField + ' or charindex('',''+'''+@slice+'''+'','','',''+Field+'','') > 0'
		end  
        set @Field =right(@Field,len(@Field)- @idx)
        if len(@Field)= 0 break
    end
	set @theStr=@theStr+' and ('+ @theField +')'
end

exec C_up_custompage1 PackegId,'AddTime','*','IntellManuSchool_CourseStatisticsV',@theStr,@page_size,@current_page,@total output
print @theStr
end

GO

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值