Method 1: the plain text method reads the file as a text file.
Int intColCount = 0;
Bool blnFlag = true;
DataTable mydt = new DataTable ("myTableName ");
DataColumn mydc;
DataRow mydr;
String strpath = ""; // cvs file path
String strline;
String [] aryline;
System. IO. StreamReader mysr = new System. IO. StreamReader (strpath );
While (strline = mysr. ReadLine ())! = Null)
{
Aryline = strline. Split (new char [] {,});
If (blnFlag)
{
BlnFlag = false;
IntColCount = aryline. Length;
For (int I = 0; I <aryline. Length; I)
{
Mydc = new DataColumn (aryline [I]);
Mydt. Columns. Add (mydc );
}
}
Mydr = mydt. NewRow ();
For (int I = 0; I <intColCount; I)
{
Mydr [I] = aryline [I];
}
Mydt. Rows. Add (mydr );
}
Method 2: Read data as a data source. Common SQL statements can be executed.
Using (OleDbConnection conn = new OleDbConnection (@ "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = C: csv; Extended Properties = Text ;"))
{
DataTable dtTable = new DataTable ();
OleDbDataAdapter adapter = new OleDbDataAdapter ("SELECT * FROM region test.csv]", conn );
Try
{
Adapter. Fill (dtTable );
}
Catch (Exception ex)
{
DtTable = new DataTable ();
}
This. GridView1.DataSource = dtTable;