1

In my website, I have many CSV files that I need to parse and insert their data into my MySQL database.

How can I parse the CSV in my website programmatically?

3
  • Very close to stackoverflow.com/questions/4748082/…. Only a few words different. Commented Jan 20, 2011 at 14:18
  • 3
    The other post is about downloading, not parsing. Commented Jan 20, 2011 at 14:21
  • 1
    this has nothing to do with asp.net, it's only about parsing. Commented Jan 20, 2011 at 14:22

2 Answers 2

6

I recommend looking at the TextFieldParserClass in .Net. You need to include

Imports Microsoft.VisualBasic.FileIO.TextFieldParser

Here's a quick sample:

Dim afile As FileIO.TextFieldParser = New FileIO.TextFieldParser(FileName)
Dim CurrentRecord As String() ' this array will hold each line of data
afile.TextFieldType = FileIO.FieldType.Delimited
afile.Delimiters = New String() {","}
afile.HasFieldsEnclosedInQuotes = True

' parse the actual file
Do While Not afile.EndOfData
    Try
        CurrentRecord = afile.ReadFields
    Catch ex As FileIO.MalformedLineException
        Stop
    End Try
Loop
Sign up to request clarification or add additional context in comments.

Comments

0

Got the Answer:

I have successfully done the parsing of my CSV file using the below code:

_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["csvFileName"];
        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[] 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);
        }
    }
    finally
    {
        conn.Close();
        MasterCalendar_DB.CloseMySQLConnection();
    }

Also you need to add the following code under Configurations tag into your web.config file:

<appSettings> <add key="csvFileName" value="<file_name>.csv" /> <add key="csvFolder" value="<filePath>"/> </appSettings>

Hope this helps someone :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.