简体   繁体   中英

How to get index ID of a specific column from CSV file

I am doing this:

using (TextFieldParser parser = new TextFieldParser(ConfigurationManager.AppSettings[Constants.FILE], Constants.ENCODING))
{
    parser.TextFieldType = FieldType.Delimited;
    parser.SetDelimiters(",");
    parser.HasFieldsEnclosedInQuotes = true;
    string[] colFields = parser.ReadFields();

    foreach (string column in colFields)
    {
        DataColumn datecolumn = new DataColumn(column);
        datecolumn.AllowDBNull = true;
        if (column == "Column001")
        {
        }
    }
}

What I want to do is when column name equals variable I want to add it to datatable column and get it's index ID(I want to know what colFields[?] have the name of variable)

If you want to know the index of the column, you should use a for loop instead:

for (int i = 0; i < colFields.Length; ++i)
{
    string column = colFields[i];
    DataColumn datecolumn = new DataColumn(column);
    datecolumn.AllowDBNull = true;
    if (column == "Column001")
    {
    }
}

As you can see, you now have the index in i .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM