我需要导入 CSV 文件,然后上传到数据表中。然后我需要验证该列是否满足要求,并确保最终用户以正确的格式导入。在 CSV 文件中必须有 3 列,即 item、price 和 measure。
当我尝试将列名与字符串进行比较时,错误就开始了,这里是代码片段。
private bool verifyColumn(DataTable dt)
{
    /* Column 
     * 1) items
     * 2) price
     * 3) measure
    */
    foreach (DataColumn col in dt.Columns)
    {  
        if (col.ColumnName.ToString() == "price")
        {
            continue;
        }
        else if (col.ColumnName.ToString() == "item")
        {
            continue;
        }
        else if (col.ColumnName.ToString() == "measure")
        {
            continue;
        }
        else
        {
            return false;
        }                           
    }
    return true;
}
在调试模式下,第一个循环是 show 'item' 但不能在 if 语句中捕获。我该如何解决这个问题?
使用A Portable and Efficient Generic Parser for Flat Files将 CSV 文件导入数据表的代码。
GenericParserAdapter paste = new GenericParserAdapter(fileName);                    
paste.FirstRowHasHeader = true;
paste.ColumnDelimiter = ';';
DataTable dt dt = paste.GetDataTable();
if (dt.Columns.Count != 3)
{ 
    MessageBox.Show(errorMessage, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
    dt.Clear();
    return;
}
// Here to do verify columт