C# 转介计数生成不计算所有转介(循环困难)

C# 转介计数生成不计算所有转介(循环困难),c#,asp.net,C#,Asp.net,我目前面临while循环的问题。这是一个场景,我需要分别计算金字塔左侧和右侧的总人数。这是我的代码: void GetLeftCount() { string TOP = Session["UserID"].ToString(); Connection.Open(); SqlCommand CMD = new SqlCommand(); CMD.Connection = Connection; CMD.CommandText = "SELECT * FR

我目前面临while循环的问题。这是一个场景,我需要分别计算金字塔左侧和右侧的总人数。这是我的代码:

void GetLeftCount()
{
    string TOP = Session["UserID"].ToString();

    Connection.Open();
    SqlCommand CMD = new SqlCommand();
    CMD.Connection = Connection;
    CMD.CommandText = "SELECT * FROM Accounts WHERE AssignedTo=@Referrer AND PyramidPosition=@Position";
    CMD.Parameters.AddWithValue("@Referrer", TOP);
    CMD.Parameters.AddWithValue("@Position", "Left");
    SqlDataReader RDR = CMD.ExecuteReader();
    while (RDR.Read())
    {
        TOP = RDR["AccountID"].ToString();

        SqlCommand CMD2 = new SqlCommand();
        CMD2.Connection = Connection;
        CMD2.CommandText = "SELECT * FROM Accounts WHERE AssignedTo=@Referrer";
        CMD2.Parameters.AddWithValue("@Referrer", TOP);
        SqlDataReader RDR2 = CMD2.ExecuteReader();
        while(RDR2.Read())
        {
            string TOP2 = RDR2["AccountID"].ToString();

            SqlCommand CMD3 = new SqlCommand();
            CMD3.Connection = Connection;
            CMD3.CommandText = "SELECT * FROM Accounts WHERE AssignedTo=@Referrer";
            CMD3.Parameters.AddWithValue("@Referrer", TOP2);
            SqlDataReader RDR3 = CMD3.ExecuteReader();
            while(RDR3.Read())
            {
                LeftCount = LeftCount + 1;
            }
            LeftCount = LeftCount + 1;
        }
        LeftCount = LeftCount + 1;
    }
    Connection.Close();
}
我现在的问题是,金字塔下的转介数量只达到一个特定的级别/层,即级别3。它不包括4级以下的转介等

我的循环有什么问题?我应该将while循环转换为“for”循环吗?因为我不知道如何在不弄乱代码的情况下做到这一点


注意:这是一个金字塔推荐投影,所有的评论和建议都非常感谢。

您可以尝试以下方法之一。不用说,在任何情况下,您都需要在AssignedTo列上建立索引

(1) 使用一个递归函数,该函数将对任何深度的间接引用进行计数。不过,我还是要小心使用它,因为太多嵌套调用可能会导致数据库瓶颈

int CountReferrers(string accountID, string position = null)
{
  int count = 0;

  // If a position argument was specified, add the filter to the query
  string cmdText = "SELECT * FROM Accounts WHERE AssignedTo=@Referrer";
  if (position != null)
    cmdText += " AND PyramidPosition=@Position";

  var cmd = new SqlCommand(cmdText, this.Connection);
  cmd.Parameters.AddWithValue("@Referrer", accountID);

  // If a position argument was specified, add the value to the parameter
  if (position != null)
    cmd.Parameters.AddWithValue("@Position", position);

  // Count the child referrers, without specifying position
  using (SqlDataReader reader = cmd.ExecuteReader())
  {
    while (reader.Read())
      count += CountReferrers(reader["AccountID"].ToString());
  }

  return count;
}

void Main()
{
  string userId = Session["UserID"].ToString();
  int referrerCount = CountReferrers(userId, "Left");
  Console.Write(referrerCount);
}
(2) 编写一个更好的SQL查询,使用联接将所有内容扁平化为单个命令。您可以添加任意数量的联接,比如最多8个联接,如果此级别没有引用,AccountID8列将为空

SELECT
    a1.AccountID AS AccountID1,
    a2.AccountID AS AccountID2,
    a3.AccountID AS AccountID3
FROM
    Accounts AS a1
    LEFT OUTER JOIN Accounts AS a2 ON a2.AssignedTo = a1.AccountID
    LEFT OUTER JOIN Accounts AS a3 ON a3.AssignedTo = a2.AccountID
WHERE
    a1.AssignedTo=@Referrer AND PyramidPosition=@Position
(3) 设计数据库以支持树检索。例如,添加一个参照者路径列,如so(字符串):

然后检索间接子对象就变得简单多了:

SELECT * FROM Accounts WHERE ReferrerPath LIKE '/1/%';

提供直接或间接引用帐户ID 1的所有行。当然,更改引用人现在需要更新所有间接引用,以便路径保持最新。

有人需要帮助吗?:)没有人想过来帮我吗?我的问题没问题,对吧?谢谢你非常有帮助的回答:)但尽管如此。。。这不起作用:(因为例如..当我得到顶部的两个引用..我还需要得到顶部左右的两个引用,依此类推,所有的小分支直到无穷大或其他…但是你对递归函数的评论:)我真的很喜欢那个:)我一直在努力学习如何使用和实现它。。。但不幸的是。。我失败了:(但第一个答案非常接近我需要首先尝试和实验的内容:)如果我完成了这项工作,我会记下答案这真的非常接近。。。我能感觉到。。。我要做的就是找到一种方法使计数器工作:)并获取主用户下的帐户数/ID:)@Doron,以及所有-
通常在同一连接上不能同时执行多个读卡器-
-非常不真实。请阅读。火星现在可以使用10年了吗?您可以轻松地在连接字符串选项中打开它。这种模式完全“正常”。默认情况下,许多应用程序和orm都使用它。加上,旧文章(s2005!),但包含@quetzalcatl-jeez。我怎么可能从来没有听说过这件事。为了避免这种情况,我经常使用透明缓冲区包装读卡器。难以置信,谢谢你终于明白了:)非常感谢你的回答:)因为你,我学会了如何使用递归函数:)
SELECT * FROM Accounts WHERE ReferrerPath LIKE '/1/%';