Shaantu

How can i read *.csv file and import data in database also read data from database and write in *.csv file


Re: Windows Forms General Read and write *.csv File

Slesh

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.




Re: Windows Forms General Read and write *.csv File

nobugz

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...





Re: Windows Forms General Read and write *.csv File

eppoeh

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





Re: Windows Forms General Read and write *.csv File

nobugz

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();
}
}
}





Re: Windows Forms General Read and write *.csv File

Shabber

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.