The following program illustrates how to read an existing csv file in C#
Step 1: Create a console application with the name 'ReadingCSVFile'. and past the following code to your program class.
Step 2: Add
one CSV file to your project, Following is a sample of csv file shown in screen
shot
Program.CS code
using System;
using System.Data;
using System.IO;
namespace ReadingCSVFile
{
class Program
{
static void Main(string[] args)
{
string[] tableName = {
"EMPLOYEE",
"DEPT"
};
for (int i = 0; i < tableName.Length; i++)
{
fnReadFile(tableName[i]);
}
}
private static void fnReadFile(string TableName)
{
string path = @"D:\Projects\ReadingCSVFile\" + TableName + ".csv";
string extension = Path.GetExtension(path);
if (extension == ".csv")
{
StreamReader objStreamReader = new StreamReader(path);
DataTable dt = null;
int RowCount = 0;
string[] ColumnNames = null;
string[] objStreamDataValues = null;
//using
while loop read the stream data till end
while (!objStreamReader.EndOfStream)
{
String objStreamRowData =
objStreamReader.ReadLine().Trim();
if (objStreamRowData.Length
> 0)
{
objStreamDataValues =
objStreamRowData.Split(',');
//Bcoz the first row contains column names, we will
poluate the column name by
//reading
the first row and RowCount(0) will be true only once
if (RowCount == 0)
{
RowCount = 1;
ColumnNames =
objStreamDataValues;
dt = new DataTable();
//using foreach looping through all the column names
foreach (string csvcolumn in ColumnNames)
{
DataColumn dtColumn = new DataColumn(csvcolumn.ToUpper().Trim('"').Trim('\'').Trim(), typeof(string));
//setting the default value of empty.string to newly
created column
dtColumn.DefaultValue = string.Empty;
//adding the newly created column to the table
dt.Columns.Add(dtColumn);
}
}
else
{
if (ColumnNames.Length ==
objStreamDataValues.Length)
{
//creates a new DataRow with the same schema as of the
DataTable (dt)
DataRow oDataRow =
dt.NewRow();
//using foreach looping through all the column names
for (int i = 0; i <
ColumnNames.Length; i++)
{
if (!string.IsNullOrEmpty(objStreamDataValues[0].Trim('"').Trim('\'').Trim()))
oDataRow[ColumnNames[i].Trim('"').Trim('\'').Trim()] = objStreamDataValues[i].Trim('"').Trim('\'').Trim().ToString() == null ? "" :
objStreamDataValues[i].Trim('"').Trim('\'').Trim().ToString();
}
//adding the newly created row with data to the
oDataTable
dt.Rows.Add(oDataRow);
}
}
}
}
//close
the objStreamReader object
objStreamReader.Close();
//release
all the resources used by the objStreamReader object
objStreamReader.Dispose();
if (dt.Rows.Count > 0)
{
fnInsertData(dt,
TableName);
}
}
}
private static void fnInsertData(DataTable dt, string TableName)
{
switch (TableName)
{
case "EMPLOYEES":
{
for (int i = 0; i < dt.Rows.Count;
i++)
{
//Your function to insert record to database
//fnInsEMPLOYEES(dt.Rows[i]);
}
break;
}
case "DEPT":
{
for (int i = 0; i < dt.Rows.Count;
i++)
{
//Your function to insert record to database
//fnInsDEPT(dt.Rows[i]);
}
break;
}
}
}
}
}
No comments:
Post a Comment