using System; using System.Data; using Microsoft.VisualBasic.FileIO; namespace ReadDataFromCSVFile { static class Program { static void Main() { string csv_file_path = @"C:\Matlab\Limits.csv"; DataTable table1 = GetDataTabletFromCSVFile(csv_file_path); string csv_file_path1 = @"C:\Matlab\Sheet1_t168h.csv"; DataTable table2 = GetDataTabletFromCSVFile1(csv_file_path1); DataTable table3 = CompareTwoDataTable(table1,table2); Console.WriteLine("Rows count:" + table3.Rows[1] ["CON_ALL:open_ADCI1_N_(ADCI1_N)@ADCI1_N[1]"].ToString()); Console.ReadLine(); } private static DataTable GetDataTabletFromCSVFile(string csv_file_path) { DataTable table1 = new DataTable("Limits"); using (TextFieldParser csvReader = new TextFieldParser(csv_file_path)) { csvReader.SetDelimiters(new string[] { "," }); csvReader.HasFieldsEnclosedInQuotes = true; string[] colFields = csvReader.ReadFields(); foreach (string column in colFields) { DataColumn datecolumn1 = new DataColumn(column); datecolumn1.AllowDBNull = true; table1.Columns.Add(datecolumn1); } while (!csvReader.EndOfData) { string[] fieldData = csvReader.ReadFields(); //Making empty value as null for (int i = 0; i < fieldData.Length; i++) { if (fieldData[i] == "") { fieldData[i] = null; } } table1.Rows.Add(fieldData); } DataRow[] rows1; rows1 = table1.Select("SKIP = '1'"); foreach (DataRow r in rows1) r.Delete(); } return table1; } private static DataTable GetDataTabletFromCSVFile1(string csv_file_path1) { DataTable table2 = new DataTable("Real"); using (TextFieldParser csvReader1 = new TextFieldParser(csv_file_path1)) { csvReader1.SetDelimiters(new string[] { "," }); csvReader1.HasFieldsEnclosedInQuotes = true; string[] colFields = csvReader1.ReadFields(); foreach (string column in colFields) { DataColumn datecolumn2 = new DataColumn(column); datecolumn2.AllowDBNull = true; table2.Columns.Add(datecolumn2); } while (!csvReader1.EndOfData) { string[] fieldData1 = csvReader1.ReadFields(); //Making empty value as null for (int i = 0; i < fieldData1.Length; i++) { if (fieldData1[i] == "") { fieldData1[i] = null; } } table2.Rows.Add(fieldData1); } } return table2; } private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2) { DataTable table3 = new DataTable(); DataRow dr = null; string filterExp = string.Empty; for (int i = 0; i < table1.Rows.Count; i++) { string col = table1.Rows[i]["Par Name"].ToString(); if (table2.Columns.Contains(col)) { if (!table3.Columns.Contains(col)) { table3.Columns.Add(col, typeof(String)); filterExp = filterExp+col+" asc ,"; } for (int j = 0; j < table2.Rows.Count; j++) { if (table3.Rows.Count != table2.Rows.Count) { dr = table3.NewRow(); table3.Rows.Add(dr); } table3.Rows[j][col] = (table2.Rows[j][col]); } } } DataView dv = new DataView(table3); filterExp =filterExp.TrimEnd(','); dv.Sort = filterExp; table3 = dv.ToTable(); return table3; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)