我有一个函数旨在从 SQL 数据库表(称为 ProductProfiles)中获取值并将它们显示在用户可以下载和查看的 .csv 文档中。结果基于下拉列表的选择(称为 ddlClassification)。该函数几乎可以完美运行,但用于将 sql 数据写入 .csv 文件的 while 循环不断重复数据。例如,如果存在 3 个条目(我们将它们称为 A、B 和 C),则 .csv 文件应该只显示 3 个条目。但是,就目前的情况而言,这 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;
}
}
}