locked
How to import csv files in C# web application and perform validations checking

    Question

  • How to import csv file in C# web application and perform validations checking .

    Please let me know the approach.

    Thanks in advance.

    Monday, March 22, 2010 11:25 AM

Answers

  • Here you go...

            private void loadCsvData(string fileName)
            {
                FileInfo fileIn = new FileInfo(fileName);
                
                StreamReader reader = fileIn.OpenText();
                string[] lineIn;
    
                while (!reader.EndOfStream)
                {
                    lineIn = reader.ReadLine().Split(',');
                    //Validate values here
                }
    
                reader.Close();
            }
    • Proposed as answer by StLuisRey Tuesday, March 23, 2010 1:39 PM
    • Marked as answer by SamAgain Monday, March 29, 2010 4:06 AM
    Monday, March 22, 2010 3:16 PM
  • Use ADO with the Microsoft.Jet.OLEDB provider:

    private static DataTable readCSV(string filename) {
      string path = System.IO.Path.GetDirectoryName(filename);
      string file = System.IO.Path.GetFileName(filename);

      DataTable dt = new DataTable();
      using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + path + "';Extended Properties='text;HDR=Yes;FMT=Delimited'")) {
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM " + file, conn);
        da.Fill(dt);
      }
      return dt;
    }

    • Proposed as answer by M.L. Somers Tuesday, March 23, 2010 12:45 PM
    • Marked as answer by SamAgain Monday, March 29, 2010 4:06 AM
    Tuesday, March 23, 2010 12:45 PM

All replies

  • Here you go...

            private void loadCsvData(string fileName)
            {
                FileInfo fileIn = new FileInfo(fileName);
                
                StreamReader reader = fileIn.OpenText();
                string[] lineIn;
    
                while (!reader.EndOfStream)
                {
                    lineIn = reader.ReadLine().Split(',');
                    //Validate values here
                }
    
                reader.Close();
            }
    • Proposed as answer by StLuisRey Tuesday, March 23, 2010 1:39 PM
    • Marked as answer by SamAgain Monday, March 29, 2010 4:06 AM
    Monday, March 22, 2010 3:16 PM
  • Use ADO with the Microsoft.Jet.OLEDB provider:

    private static DataTable readCSV(string filename) {
      string path = System.IO.Path.GetDirectoryName(filename);
      string file = System.IO.Path.GetFileName(filename);

      DataTable dt = new DataTable();
      using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + path + "';Extended Properties='text;HDR=Yes;FMT=Delimited'")) {
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM " + file, conn);
        da.Fill(dt);
      }
      return dt;
    }

    • Proposed as answer by M.L. Somers Tuesday, March 23, 2010 12:45 PM
    • Marked as answer by SamAgain Monday, March 29, 2010 4:06 AM
    Tuesday, March 23, 2010 12:45 PM
  • How to fill all data from csv into untyped dataset?here i am not able to insert string into datetime column.it gives null if first row is datetime and next column is string .
    Sunday, January 16, 2011 2:48 PM
  • It is a good way, but this does not work on 64bit machine.
    I prefer StreamReader with Regex.
    Monday, March 05, 2012 6:28 PM
  • Maybe this helps too.

            void lines(string path)
            {
                string quoteText = "\"";
                string fieldDelimiter = ",";

                Regex regex = new Regex(String.Format("{0}(?<field>[^{0}]*){0}|(?<field>[^{1}]*)", quoteText, fieldDelimiter), RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
                int fieldGroupIndex = regex.GroupNumberFromName("field");
                using (StreamReader csv = new StreamReader(path))
                {
                    string lineText;
                    List<string> row = new List<string>();
                    while ((lineText = csv.ReadLine()) != null)
                    {
                        MatchCollection m = regex.Matches(lineText);
                        for (int i = 0; i < m.Count; i += 2)
                        {
                            row.Add(m[i].Groups[fieldGroupIndex].Value);
                            // check the values.
                        }

                        row.Clear();
                    }
                }
            }

    Monday, March 05, 2012 6:36 PM