while循环不断返回重复信息

时间:2014-06-19 20:34:12

标签: c# sql while-loop idatareader textwriter

我有一个函数,用于从SQL数据库表(称为ProductProfiles)获取值,并将它们显示在用户可以下载和查看的.csv文档中。结果基于下拉列表的选择(称为ddlClassification)。该函数几乎完美地工作,但用于将sql数据写入.csv文件的while循环不断重复数据。例如,如果存在3个条目(我们将其称为A,B和C),则.csv文件应仅显示3个条目。但是,就像现在这样,这三个条目在.csv文件中重复多次,这是不正确的。

我不确定我在该功能中缺少什么来结束这个重复的问题。

这是功能

private void WriteProductProfile(string ProfileID, ref TextWriter tw)
{
    string CatList = GetCatList();

    string sqlQuery = @"SELECT [ProfileID], [Name], [Description], [SpeciesLink],
          [LineDraw], [LineDrawThumbnail], [ProfileThumbnail], [ComponentThickness],
          [ComponentWidth], [ComponentFactor], [FinishedThickness], [FinishedWidth],
          ISNULL([ClassificationID],-1) as ClassificationID, [StockOrCust],
          [Visibility], [Notes], [OrderBy]
        FROM ProductProfile
        WHERE ClassificationID = @ClassificationID";
    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
    {
        SqlCommand cmd = new SqlCommand(sqlQuery, cn);
        cmd.Parameters.Add(new SqlParameter("@ClassificationID", ddlClassification.SelectedValue));
        cmd.CommandType = CommandType.Text;
        cn.Open();
        using (IDataReader reader = cmd.ExecuteReader())
        {
            while(reader.Read())
            {
                string myLine = reader["ProfileID"] + "\t"
                    + ImportHelper.CleanHTMLText(reader["Name"].ToString()) + "\t"
                    + ImportHelper.CleanHTMLText(reader["Description"].ToString()) + "\t"
                    + reader["SpeciesLink"] + "\t"
                    + reader["LineDraw"] + "\t"
                    + reader["LineDrawThumbnail"] + "\t"
                    + reader["ProfileThumbnail"] + "\t"
                    + reader["ComponentThickness"] + "\t"
                    + reader["ComponentWidth"] + "\t"
                    + reader["ComponentFactor"] + "\t"
                    + reader["FinishedThickness"] + "\t"
                    + reader["FinishedWidth"] + "\t"
                    + reader["ClassificationID"] + "\t"
                    + reader["StockOrCust"] + "\t"
                    + reader["Visibility"] + "\t"
                    + ImportHelper.CleanHTMLText(reader["Notes"].ToString()) + "\t"
                    + reader["OrderBy"] + "\t";
                tw.WriteLine(myLine);
            }
        }
        cn.Close();
    }
}

这是调用WriteProductProfile函数的代码

private void ProcessCategory(Category MainCat, ref TextWriter tw, ref string ProdUseList)
{
    string sqlQuery = "SELECT [ProfileID], [ClassificationID] FROM baird_ProductProfile WHERE ClassificationID = @ClassificationID";
    string profId = "";
    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
    {
        SqlCommand cmd = new SqlCommand(sqlQuery, cn);
        cmd.Parameters.Add(new SqlParameter("@ClassificationID", ddlClassification.SelectedValue));
        cmd.CommandType = CommandType.Text;
        cn.Open();
        using (IDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                profId = reader["ProfileID"].ToString();
            }
        }
        cn.Close();
    }

    foreach (CatalogNode subCat in MainCat.CatalogNodes)
    {
        //process the children items
        switch (subCat.CatalogNodeType)
        {
            case CatalogNodeType.Category:
                ProcessCategory((Category)subCat.ChildObject, ref tw, ref ProdUseList);
                break;
            case CatalogNodeType.Product:
                if (AddProduct(profId, ref ProdUseList))
                    WriteProductProfile(profId, ref tw);
                break;
        }
    }
}

0 个答案:

没有答案