C# 递归CTE在ASP.NET中不使用SqlDataSource

C# 递归CTE在ASP.NET中不使用SqlDataSource,c#,asp.net,sql-server,common-table-expression,C#,Asp.net,Sql Server,Common Table Expression,我的ASP.NET应用程序中有一个SqlDataSource。查询在SQLServer2008ManagementStudio中运行良好 但是在ASP.NET应用程序中,它不返回任何值 <asp:SqlDataSource ID="ChartOfAccountsFillerDS" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectC

我的ASP.NET应用程序中有一个
SqlDataSource
。查询在SQLServer2008ManagementStudio中运行良好

但是在ASP.NET应用程序中,它不返回任何值

 <asp:SqlDataSource ID="ChartOfAccountsFillerDS" runat="server"
      ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
      SelectCommand = "with cte as(select ChartOfAccountsID, Parent_ChartOfAccountsID, GL_Code, AccountName, 1 as [Level],
                            right('000' + cast(row_number()over(partition by Parent_ChartOfAccountsID order by GL_Code) as varchar(max)), 3) as [Path]
                            from ChartOfAccounts where Parent_ChartOfAccountsID is null
                            union all select t.ChartOfAccountsID, t.Parent_ChartOfAccountsID, t.GL_Code, t.AccountName, [Level] + 1,
                            [Path] + '.' + right('000' + cast(row_number()over(partition by t.Parent_ChartOfAccountsID order by t.GL_Code) as varchar(max)), 3)
                            from cte join ChartOfAccounts t on t.Parent_ChartOfAccountsID = cte.ChartOfAccountsID)
                            select ChartOfAccountsID as AccountID, Parent_ChartOfAccountsID as ParentID, GL_Code, (REPLICATE(CHAR(160), ([Level] -1)*5) +  AccountName) AccountName, [Level], [Path] from cte  order by [Path]">
</asp:SqlDataSource>


SqlDataSource
不支持CTE表达式IMHO应该没有任何理由。你真的确定你的连接字符串、你连接的数据库和模式是正确的吗?是的,它可以很好地处理简单的选择查询。我在C#中尝试了SqlDataReader。它在那里也不起作用。Read()函数返回false。@Andrei这是递归CTE。我怀疑这是一个原因,但在您的查询中我没有看到。我只能建议把东西拿出来直到它运行,然后再把东西放回去直到它坏掉。对不起…:(当然,我不反对这一努力,我试图为您提供一种消除可能原因的方法。只需在cte中不使用union all构造,也不使用执行
复制
函数的字段,所以只使用普通字段即可。如果这样做有效,请将union all构造添加回cte中,而不使用se函数。如果这样做有效的话,添加带有函数的字段。在这个过程中的某个地方,你的东西会断开。知道什么时候断开会很有趣。