3

解决..

这已解决,因为我犯了一个愚蠢的错误。我没有初始化字符串生成器 StringBuilder sb = new StringBuilder();

我在我的网站中使用以下 CTE 查询来使用 c# 获取数据。但它在 for 循环中的 if 语句中给出了错误

错误信息

System.NullReferenceException 未被用户代码处理 Message=Object 引用未设置为对象的实例。

    String strSqlCTE;

    strSqlCTE= " DECLARE @PageId int ";
    strSqlCTE += " SET @PageId =" + pageid + "; ";
    strSqlCTE += " WITH RecursiveTable (PageId, PageName, PageInternalLink, PageInternalLinkUrl, PageInheritance, Level) ";
    strSqlCTE += " AS(SELECT      tt.PageId,  tt.PageName, tt.PageInternalLink, tt.PageInternalLinkUrl, tt.PageInheritance,  0 AS Level ";
    strSqlCTE += " FROM pg_Pages AS tt WHERE PageId = @PageId ";
    strSqlCTE += " UNION ALL ";
    strSqlCTE += " SELECT tt.PageId,  tt.PageName, tt.PageInternalLink, tt.PageInternalLinkUrl, tt.PageInheritance, Level + 1 ";
    strSqlCTE += " FROM pg_Pages AS tt ";
    strSqlCTE += " INNER JOIN RecursiveTable rt ON rt.PageInheritance = tt.PageId  ) ";
    strSqlCTE += " SELECT * FROM RecursiveTable ORDER BY Level DESC ";

    DataSet dsBC = new DataSet();
    dsBC = DataProvider.Connect_Select(strSqlCTE);
    if (dsBC.Tables[0].Rows.Count > 0)
    {
        int RowCount = dsBC.Tables[0].Rows.Count;
        StringBuilder sb = null;
        for ( int i = 0; i <= RowCount; i++)
        {
            if (i == RowCount)
            {
                sb.Append("<a href='" + dsBC.Tables[0].Rows[0]["PageInternalLinkUrl"].ToString() + "'>" + dsBC.Tables[0].Rows[0]["PageName"].ToString().ToUpper() + "</a>");
            }
            else
            {
                sb.Append("<a href='" + dsBC.Tables[0].Rows[0]["PageInternalLinkUrl"].ToString() + "'></a> ● ");
            }

        }

        ltrBreadCrumb.Text = sb.ToString();

    }

我的 CTE 查询正在运行,下面是实际查询

DECLARE @PageId int
SET @PageId = 31;
WITH RecursiveTable (PageId, PageName, PageInternalLink, PageInternalLinkUrl, PageInheritance, Level)
AS(
   --Anchor
    SELECT      tt.PageId,  tt.PageName, tt.PageInternalLink, tt.PageInternalLinkUrl, tt.PageInheritance,  0 AS Level
    FROM pg_Pages AS tt
    WHERE PageId = @PageId
    UNION ALL
   --Recursion
    SELECT tt.PageId,  tt.PageName , tt.PageInternalLink, tt.PageInternalLinkUrl, tt.PageInheritance, Level + 1
    FROM pg_Pages AS tt
    INNER JOIN RecursiveTable rt ON rt.PageInheritance = tt.PageId
)
SELECT * FROM RecursiveTable ORDER BY Level DESC

这在管理工作室中运行时效果很好,但以下语句会生成错误

sb.Append("<a href='" + dsBC.Tables[0].Rows[0]["PageInternalLinkUrl"].ToString() + "'></a> ● ");

dsBC.Tables[0].Rows.Count 语句根据输入返回正确的行号。问题似乎在于它无法识别 CTE 查询中存在的列名。

我做错了什么请纠正我的问题。

4

1 回答 1

0

这已解决,因为我犯了一个愚蠢的错误。我错过了初始化字符串生成器

StringBuilder sb = new StringBuilder();

String strSqlCTE;

    strSqlCTE= " DECLARE @PageId int ";
    strSqlCTE += " SET @PageId =" + pageid + "; ";
    strSqlCTE += " WITH RecursiveTable (PageId, PageName, PageInternalLink, PageInternalLinkUrl, PageInheritance, Level) ";
    strSqlCTE += " AS(SELECT      tt.PageId,  tt.PageName, tt.PageInternalLink, tt.PageInternalLinkUrl, tt.PageInheritance,  0 AS Level ";
    strSqlCTE += " FROM pg_Pages AS tt WHERE PageId = @PageId ";
    strSqlCTE += " UNION ALL ";
    strSqlCTE += " SELECT tt.PageId,  tt.PageName, tt.PageInternalLink, tt.PageInternalLinkUrl, tt.PageInheritance, Level + 1 ";
    strSqlCTE += " FROM pg_Pages AS tt ";
    strSqlCTE += " INNER JOIN RecursiveTable rt ON rt.PageInheritance = tt.PageId  ) ";
    strSqlCTE += " SELECT * FROM RecursiveTable ORDER BY Level DESC ";

    DataSet dsBC = new DataSet();
    dsBC = DataProvider.Connect_Select(strSqlCTE);
    if (dsBC.Tables[0].Rows.Count > 0)
    {
        int RowCount = dsBC.Tables[0].Rows.Count;
        StringBuilder sb = new StringBuilder();
        for ( int i = 0; i <= RowCount; i++)
        {
            if (i == RowCount)
            {
                sb.Append("<a href='" + dsBC.Tables[0].Rows[i]["PageInternalLinkUrl"].ToString() + "'>" + dsBC.Tables[0].Rows[i]["PageName"].ToString().ToUpper() + "</a>");
            }
            else
            {
                sb.Append("<a href='" + dsBC.Tables[0].Rows[i]["PageInternalLinkUrl"].ToString() + "'></a> ● ");
            }

        }

        ltrBreadCrumb.Text = sb.ToString();

    }
于 2012-04-19T07:21:08.920 回答