简体   繁体   中英

Getting the top row from a CSV when it is not a header row

In my website I am reading a CSV file and parsing it. Now the CSV does not have a column names. It is simply a raw list of comma seperated values.

I take this file and use the ODBCDataReader class to read the rows.

The problem is that when I retrieve the first value it skips the first row of the CSV. This is probably because it considers first row as column header. But in my case there are no column headers. So every time my first row is skipped.

How can I retrieve the first row of my CSV?

Here is the screenshot of my CSV:

替代文字

Here is the code that I am using to parse the CSV.

public string CsvParser()    
{    
    int _nNrRowsProccessed = 0;
    string connectionString = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + ConfigurationManager.AppSettings["CSVFolder"] + ";";     
    OdbcConnection conn = new OdbcConnection(connectionString);     
    try
    {
        conn.Open();

        string strFileName = ConfigurationManager.AppSettings["CSVFile"];
        string strSQL = "Select * from " + strFileName;

        OdbcCommand cmd = new OdbcCommand();
        cmd.Connection = conn;
        cmd.CommandText = strSQL;
        cmd.CommandType = CommandType.Text;

        OdbcDataReader reader = cmd.ExecuteReader();
        string strLine = null;

        // MasterCalendar_DB.OpenMySQLConnection();

        while (reader.Read())
        {
            // insert data into mastercalendar
            strLine = reader[0].ToString();
            string strLine1 = reader[1].ToString();
            string strLine2 = reader[2].ToString();
            string strLine3 = reader[3].ToString();
            string[] arLine = strLine.Split(';');

           // string strAgencyPropertyID = arLine[0];
           // DateTime dt = DateTime.Parse(arLine[1]);
           // Int64 nDate = (Int64)Util.ConvertToUnixTimestamp(dt);
           // String strAvailability = (arLine[2]);

            _nNrRowsProccessed++;
           // MasterCalendar_DB.Insert(strAgencyPropertyID, nDate, strAvailability);
        }     
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        conn.Close();
       // MasterCalendar_DB.CloseMySQLConnection();
    }
    return "Success";
}

You want to have a look at the page of the Text-Driver over at connectionstrings.org .

Basically, you create a schema.ini in the same directory, which holds varies options. One of them is the ColNameHeader option, which takes a boolean.

Example from the site:

[customers.txt]
Format=TabDelimited
ColNameHeader=True
MaxScanRows=0
CharacterSet=ANSI

Check this: Schema.ini File (Text File Driver)

You may need to set ColNameHeader = false

Reference Microsoft.VisualBasic and you can use TextFieldParser , which almost certainly has less dependencies than your proposed method.

using (var parser =
    new TextFieldParser(@"c:\data.csv")
        {
            TextFieldType = FieldType.Delimited,
            Delimiters = new[] { "," }
        })
{
    while (!parser.EndOfData)
    {
        string[] fields;
        fields = parser.ReadFields();
        //go go go!
    }
}

Quick and dirty solution:

string strLine  = reader.GetName(0);
string strLine1 = reader.GetName(1);
string strLine2 = reader.GetName(2);
string strLine3 = reader.GetName(3);

reader.GetName(int i); // Gets the name of specified column.

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