none
Read and write *.csv File RRS feed

  • Question

  • How can i read *.csv file and import data in database also read data from database and write in *.csv file
    Monday, September 11, 2006 12:19 PM

Answers

  • A *.csv file is normally just a text file with a semicolon as the delimiter and a crlf as limiter for rows. So you use a textreader/textwriter to read/write the file.
    Reading *.csv:
    Read every row of the file and "split" the resulting string with the delimiter ";". The result is an array of all the data in the row.
    Writing *.csv:
    Write every single dataset followed by a semicolon in a string. Write the string via "writeline" in the file. Proceed with the next row.

    Hope this helps.
    Monday, September 11, 2006 12:25 PM

All replies

  • A *.csv file is normally just a text file with a semicolon as the delimiter and a crlf as limiter for rows. So you use a textreader/textwriter to read/write the file.
    Reading *.csv:
    Read every row of the file and "split" the resulting string with the delimiter ";". The result is an array of all the data in the row.
    Writing *.csv:
    Write every single dataset followed by a semicolon in a string. Write the string via "writeline" in the file. Proceed with the next row.

    Hope this helps.
    Monday, September 11, 2006 12:25 PM
  • If you're using VB.NET, it has built-in support for parsing .csv files.  If not, you can always add a reference to Microsoft.VisualBasic...
    Monday, September 11, 2006 1:10 PM
    Moderator
  •  nobugz wrote:
    If you're using VB.NET, it has built-in support for parsing .csv files.  If not, you can always add a reference to Microsoft.VisualBasic...

    please finish your sentence... really curious

    Thursday, September 21, 2006 2:59 PM
  • I'm assuming you use C#.  First, add a reference to the Microsoft.VisualBasic assembly with Project + Add Reference.  Now you can use the Microsoft.VisualBasic.FileIO.TextFieldParser class.  Here's some sample code:

    using System;
    using System.Windows.Forms;
    using Microsoft.VisualBasic.FileIO;

    namespace WindowsApplication1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
          TextFieldParser prs = new TextFieldParser(@"c:\temp\test.csv");
          prs.TextFieldType = FieldType.Delimited;
          prs.SetDelimiters(",");
          while (!prs.EndOfData) {
            string[] flds = prs.ReadFields();
            foreach (string fld in flds) Console.Write("{0}\t", fld);
            Console.WriteLine();
          }
          prs.Close();
        }
      }
    }
    Thursday, September 21, 2006 4:02 PM
    Moderator
  • Hi,

     

    I am unable to above mentioned reference. Please let me know how can i find it. As currently i cant see it in my add reference tab.

     

    Thanks,

    Shabber.

     

    Thursday, September 20, 2007 3:07 AM
  • Just use the Visual Basic reference.

     

    Friday, June 20, 2008 2:27 PM
  • Or simply write your own parser as shown in the article Reading and Writing CSV Files in C#. It isn't that hard.

    Jonathan Wood • Black Belt Coder

    Thursday, August 16, 2012 4:46 PM