DataReader.read 上的嵌套 While 循环在第二个父循环上转义以填充组合框 C#

Nested While loop on DataReader.read escaping on second parent loop to fill a combo box C#

我有一个 TopCategory Table,它本身带有 ParentId 字段,然后是 RootCategory,ParentId 引用 TopCategory。由于 Rootcategory 是唯一链接到产品 table 的类别。我正在尝试在添加新产品时生成单个组合框,它显示嵌套列表或包含所有父类别和子类别名称的列表。 我非常成功地获得了预期的结果,但是在完成第一个父 While 的整个循环后,我在内部 While 循环中获得顶级类别正在转义。因此在组合框中只导致一项。

下面是一个想要的结果的函数和图片

private void FillCBoxCategories()
    {
        using (SqlConnection con = new SqlConnection(ConnectionString))
        {
            SqlCommand cmdGetRootCategories = new SqlCommand("select * from RootCategories", con);
            SqlCommand cmdGetTopCategories = new SqlCommand("select * from TopCategories", con);


            SqlDataReader rCatReader, tCatReader;
            try
            {
                con.Open();
                rCatReader = cmdGetRootCategories.ExecuteReader();
                tCatReader = cmdGetTopCategories.ExecuteReader();

                int rCatIdOrdinal = rCatReader.GetOrdinal("Id");
                int rCatNameOrdinal = rCatReader.GetOrdinal("RCatName");
                int rCatAbbreOrdinal = rCatReader.GetOrdinal("Abbre");
                int rCatParentIdOrdinal = rCatReader.GetOrdinal("TopCategoryId");

                int tCatIdOrdinal = tCatReader.GetOrdinal("Id");
                int tCatNameOrdinal = tCatReader.GetOrdinal("TCatName");
                int tCatAbbreOrdinal = tCatReader.GetOrdinal("Abbre");
                int tCatParentIdOrdinal = tCatReader.GetOrdinal("ParentCategoryId");

                while (rCatReader.Read())
                {
                    int rCatId = rCatReader.GetInt32(rCatIdOrdinal);
                    string rCatName = rCatReader.GetString(rCatNameOrdinal);
                    string rCatAbbre = rCatReader.GetString(rCatAbbreOrdinal);
                    int rCatParentId = rCatReader.GetInt32(rCatParentIdOrdinal);

                    while (tCatReader.Read())
                    {
                        string TopCatParent = "";
                        int tCatParentId = 0;
                        int tCatId = tCatReader.GetInt32(tCatIdOrdinal);
                        string tCatName = tCatReader.GetString(tCatNameOrdinal);
                        string tCatAbbre = rCatReader.GetString(tCatAbbreOrdinal);
                        if (tCatReader.IsDBNull(tCatParentIdOrdinal) ==false)
                        {
                            tCatParentId = tCatReader.GetInt32(tCatParentIdOrdinal);
                        }                         

                        if (tCatParentId > 0)
                        {
                            SqlCommand cmdGetParentTopCategories = new SqlCommand("select TCatName from TopCategories where Id='" + tCatParentId + "'", con);
                            TopCatParent = cmdGetParentTopCategories.ExecuteScalar().ToString() + " -- ";
                        }

                        if (tCatId == rCatParentId)
                        {
                            cbBoxProductCategory.Items.Add(TopCatParent + tCatName + " -- " + rCatName);
                            cbBoxProductCategory.ValueMember = rCatId.ToString();
                        }

                    }

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Add Product Form Combobox Root cat data: line 95", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


    }

期望的结果

得到结果

您应该尝试让循环调用方法而不是嵌套的 while 循环...

尝试这样的事情。

public void PartOne() {
while (rCatReader.Read())
            {
                int rCatId = rCatReader.GetInt32(rCatIdOrdinal);
                string rCatName = rCatReader.GetString(rCatNameOrdinal);
                string rCatAbbre = rCatReader.GetString(rCatAbbreOrdinal);
                int rCatParentId = rCatReader.GetInt32(rCatParentIdOrdinal);
        string data = rCatId + "," + rCatName+ "," + rCatAbbre+ "," + rCatParentId;
               PartTwo(data);

                }
}

public string PartTwo(string data) {
    while (tCatReader.Read())
                    {
            string source = data;
            string[] stringSeparators = new string[] {","};
                string[] result;
            result = source.Split(stringSeparators, 
                            StringSplitOptions.RemoveEmptyEntries);
                    string TopCatParent = "";
                    int tCatParentId = 0;
                    int tCatId = tCatReader.GetInt32(tCatIdOrdinal);
                    string tCatName = tCatReader.GetString(tCatNameOrdinal);
                    string tCatAbbre = rCatReader.GetString(tCatAbbreOrdinal);
                    if (tCatReader.IsDBNull(tCatParentIdOrdinal) ==false)
                    {
                        tCatParentId = tCatReader.GetInt32(tCatParentIdOrdinal);
                    }                         

                    if (tCatParentId > 0)
                    {
                        SqlCommand cmdGetParentTopCategories = new SqlCommand("select TCatName from TopCategories where Id='" + tCatParentId + "'", con);
                        TopCatParent = cmdGetParentTopCategories.ExecuteScalar().ToString() + " -- ";
                    }

                    if (tCatId == rCatParentId)
                    {
                        cbBoxProductCategory.Items.Add(TopCatParent + tCatName + " -- " + result[1]);
                        cbBoxProductCategory.ValueMember = result[0].ToString();
                    }

            }