Teddy Ortega

Teddy Ortega

  • NA
  • 1
  • 2.5k

Parsing CSV files and data manipulation.

Nov 22 2006 12:43 PM

Hello C# code experts,

I am building a C# program which requires that I parse a CSV formatted database, store the data into a two dimension array whose elements I could access and do data manipulations on.

The problem is how do I store them and how do I access them without using a standard database software like Access, Oracle or SQL Server to do data manipulations?

Below is my attempt at this.  What am I doing wrong?  Also I want to produce an output csv file.  How would I do it?


{

//Create an Arraylist

System.Collections.ArrayList myArraylist = new System.Collections.ArrayList();

//Create a new DataSet

DataSet myDataSet = new DataSet();

//Create a DataTable to contain the data

DataTable GNGTable2 = new DataTable();

//Creates and Names 6 columns and adds them to GNGTable1

myDataSet.Tables.Add("GNGTable2");

//Declare item property for later access

 

DataColumn aColumn;

for(int counter=0; counter < 6; counter++)

{

aColumn = new DataColumn("Column " + counter.ToString());

myDataSet.Tables["GNGTable2"].Columns.Add(aColumn);

}

// Create the StreamReader to read the streamreader class

System.IO.StreamReader myReader = new System.IO.StreamReader("C:\\GNGFile.csv");

string myString;

//Check to see if the StreamReader has reached the end of the record

string strFirstName;

string strLastName;

string strAddress;

string strCity;

string strState;

string strZip;

string[] fields;

while (myReader.Peek() != -1)

{

//Reads a line of data from the text file

myString = myReader.ReadLine();

//Assign output to myString

fields = myString();

strFirstName = fields[1];

strLastName = fields[2];

strAddress = fields[3];

strCity = fields[4];

strState = fields[5];

strZip = fields[6];

//Use the StringSplit method to create an array of strings as an entry

//that represents each field in a row. The array is then added

//as a new DataRow in GNGTable1

myDataSet.Tables["GNGTable2"].Rows.Add(myString.Split(char.Parse(",")));

//myArraylist.Add(myDataSet);

}

//myAdapter.GetFillParameters(myDataSet);

dataGrid1.DataSource = myDataSet(strFirstName,strLastName,strAddress,strCity,strState,strZip);

}



Would really appreciate your help,