Groups > Asp .Net > Visual Studio 2005 > Re: Validate a CSV file ?VS 2005




Validate a CSV file ?VS 2005

Validate a CSV file ?VS 2005
Thu, 27 Mar 2008 16:45:02 +000
Hi all,

I need to validate some CSV files.They exist in a local folder and can have any
name. eg: eir102.csv, eir103.csv

I want to check for any csv files in a folder and validate..IF Validation ok
then save to good folder IF Fail then save to bad folder.

Anyone know a good example of reading a csv file and then checking that the file
is in the correct format:

I have looked this up and using streamreader/writer and regex may be what I need
to use but I need a good example.

eg: (1) check fields lengths between commas are of a certain length

      (2)check comma count .ie: that the csv contains the correct amount of
fields (whether empty or not )

 

Please point me in the right direction.

Ray..
Post Reply
Re: Validate a CSV file ?VS 2005
Fri, 30 May 2008 10:30:12 +000
Hi,

CSV Files could be read using ADO dot net.

Use the OleddbConnection to connect to the CSV File and then you could read the
data much like sql like query.

Codestring directoryName = @"C:\";

 string connectionString =
string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=;Extended
Properties=Text;", directoryName);OleDbConnection connection =
newOleDbConnection(connectionString);

connection.Open();

DataTable dt = newDataTable();

// here the file name is the name of the csv file (not fully qualified.)string
fileName = "SampleCSV.csv";

OleDbDataAdapter dataAdapter = newOleDbDataAdapter(string.Format("Select *
from ", fileName), connection);

dataAdapter.Fill(dt);

// count will give the number of columns in the csv file;

//each column can be acessed by

//dt.Columns[0] , dt.Columns[1] etc

 int count = dt.Columns.Count;

 Please add the namespace 

using System.Data.OleDb;

for using the Oledb.
Post Reply
about | contact