I have taken over an application written by another developer, which reads data from a database, and exports it.
The developer used DataTables and DataAdaptors.
So,
_dataAdapter = new SqlDataAdapter("Select * From C....", myConnection);
and then
ExtractedData = new DataTable("CreditCards");
_dataAdapter.Fill(ExtractedData);
ExtractedData is then passed around to do different functions.
I have now been told that I need to, in addition to this, get the same format of data from some comma separated text files. The application does the same processing - it's just getting the data from two sources.
So, I am wondering if I can get the data read into a DataTable, as above, and then ADD more records from a CSV file.
Is this possible?
You might need to use this function to read the data into DataTable
from the file.
public DataTable GetDataSourceFromFile(string fileName)
{
DataTable dt = new DataTable("CreditCards");
string[] columns = null;
var lines = File.ReadAllLines(fileName);
// assuming the first row contains the columns information
if (lines.Count() > 0)
{
columns = lines[0].Split(new char[] { ',' });
foreach (var column in columns)
dt.Columns.Add(column);
}
// reading rest of the data
for (int i = 1; i < lines.Count(); i++)
{
DataRow dr = dt.NewRow();
string[] values = lines[i].Split(new char[] { ',' });
for (int j = 0; j < values.Count() && j < columns.Count(); j++)
dr[j] = values[j];
dt.Rows.Add(dr);
}
return dt;
}