using System;
using System.Data;
using System.IO;
class Class1{
static void Main(string[] args){
DataSet myDataSet = GetData();
foreach (DataColumn c in myDataSet.Tables["TheData"].Columns){
Console.Write("{0,-20}",c.ColumnName);
}
Console.WriteLine();
foreach (DataRow r in myDataSet.Tables["TheData"].Rows)
{
foreach (DataColumn c in myDataSet.Tables["TheData"].Columns)
{
Console.Write("{0,-20}",r[c]);
}
Console.WriteLine();
}
}
private static DataSet GetData(){
string strLine;
string[] strArray;
char[] charArray = new char[] {','};
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("TheData");
FileStream aFile = new FileStream("csv.txt",FileMode.Open);
StreamReader sr = new StreamReader(aFile);
strLine = sr.ReadLine();
strArray = strLine.Split(charArray);
for(int x=0;x<=strArray.GetUpperBound(0);x++) {
dt.Columns.Add(strArray[x].Trim());
}
strLine = sr.ReadLine();
while(strLine != null) {
strArray = strLine.Split(charArray);
DataRow dr = dt.NewRow();
for(int i=0;i<=strArray.GetUpperBound(0);i++) {
dr[i] = strArray[i].Trim();
}
dt.Rows.Add(dr);
strLine = sr.ReadLine();
}
sr.Close();
return ds;
}
}
// File: csv.txt
/*
1,2,3,4
5,6,7,8
*/
|