How to parse a CSV file in an ASP.NET website?

后端 未结 3 1664
别那么骄傲
别那么骄傲 2021-01-07 08:35

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条回答
  • 2021-01-07 08:55

    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 :)

    0 讨论(0)
  • 2021-01-07 09:02

    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
    
    0 讨论(0)
  • 2021-01-07 09:02

    Here is a pretty good CSV parser. You'll just have to loop through the dataset to put it back into the DB.

    http://www.codeproject.com/KB/database/CsvReader.aspx

    0 讨论(0)
提交回复
热议问题