I'm new to C# and I'm trying to figure out how to create one CSV per folder using data from a SQL database. I have several large SQL tables that are named by year(Ex. Claims_2001, Claims_2002, etc..). I will need to retrieve the data for 5 years from the database, create a folder for each year, and place one CSV file with the data for the year in the folder.
For example, I will need loop through 12 tables of data. Once the data for the first table is retrieved from the database, the first folder(folder_1_2001) would need to be created. The CSV file for Claims_2001 would need to created inside of folder_1_2001. This process would continue until all 12 folders containing 12 CSV files were created.
I'm currently using a datatable and CsvHelper to create CSV files. I'm not sure about how to loop and create each folder with a CSV file for each year.
Also not sure if it would be best to create individual SQL select statements to retrieve data from each table or if it would be best to insert all of the data into one SQL table and use the year column to create individual folders and CSV files.
Any help is appreciated.
For example, I will need loop through 12 tables of data. Once the data for the first table is retrieved from the database, the first folder(folder_1_2001) would need to be created. The CSV file for Claims_2001 would need to created inside of folder_1_2001. This process would continue until all 12 folders containing 12 CSV files were created.
I'm currently using a datatable and CsvHelper to create CSV files. I'm not sure about how to loop and create each folder with a CSV file for each year.
Also not sure if it would be best to create individual SQL select statements to retrieve data from each table or if it would be best to insert all of the data into one SQL table and use the year column to create individual folders and CSV files.
Any help is appreciated.
C#:
using( var dt = new DataTable() )
{
dt.Load( dataReader );
foreach( DataColumn column in dt.Columns )
{
csv.WriteField( column.ColumnName );
}
csv.NextRecord();
foreach( DataRow row in dt.Rows )
{
for( var i = 0; i < dt.Columns.Count; i++ )
{
csv.WriteField( row[i] );
}
csv.NextRecord();
}
}