Codes to read the CSV data using ODBC Connection-
Input File:
File's Encoding format should be ANSI as below
class Student
{
public string CollegeId { get; set; }
public string AdmissionDate { get; set; }
public string CandidateId { get; set; }
public string MobileNum { get; set; }
}
On the button Click I am calling the code to read the CSV data
private void btnTest_Click(object sender, RoutedEventArgs e)
{
string fileName = @"CSVData.csv";
//Create a ODBC TEXT File connection
string connectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};defaultdir=tempFolder;driverid=27;extensions=csv;
fil=text;maxbuffersize=2048;pagetimeout=5".Replace(" tempFolder", @"C:\Personal\TrialApps\");
string query = string.Format("SELECT * FROM `{0}`", fileName);
List lstStudentInfo = new List();
using (var odbcConnection = new OdbcConnection(connectionString))
{
odbcConnection.Open();
using (var odbcCommand = new OdbcCommand(query, odbcConnection))
{
var dataReader = odbcCommand.ExecuteReader(CommandBehavior.CloseConnection);
while (dataReader.Read())
{
string college_id=dataReader["COLLEGE_ID"].ToString();
string admission_date = dataReader["ADMISSION_DATE"].ToString();
string candidate_id = dataReader["CANDIDATE_ID"].ToString();
string mobile_num = dataReader["MOBILE_NUM"].ToString();
lstStudentInfo.Add(new Student() { CollegeId=college_id,AdmissionDate=admission_date,
CandidateId=candidate_id,MobileNum=mobile_num});
}
}
}
dtGridCSVData.ItemsSource = lstStudentInfo;
}
Xaml Code for the UI Grid:
Output:
Cheers!