How To Read Data From Csv File in C# ?
How To Read Data From Csv File in Asp.net ?
How To Read Data From Csv File in using Odbc ?
I am going to show you how to read a CSV File in C# ,Asp.net .Just Follow the following steps.
Step : -1
Create a webform which contains one FileUpload ,Button and a Gridview
<asp:FileUpload ID="fu_csv" runat="server" />
<asp:Button ID="btn_Upload" runat="server" Text="Upload" OnClick="btn_Upload_Click"/>
<asp:GridView ID="gv_csv" runat="server" HeaderStyle-BackColor="BurlyWood"></asp:GridView>
Step :-2
Then Go to Code behind and write the following Code
protected void btn_Upload_Click(object sender, EventArgs e)
{
fu_csv.SaveAs(Server.MapPath("~/upload/") + fu_csv.FileName);
string csv_path = Server.MapPath("~/upload/") + fu_csv.FileName;
ReadCVS( Server.MapPath("~/upload/"),fu_csv.FileName);
}
public void ReadCVS(string dir_path,string filename)
{
DataSet ds = new DataSet();
try
{
// Creates and opens an ODBC connection
string ConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + dir_path + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
string qry_select;
OdbcConnection conn;
conn = new OdbcConnection(ConnString.Trim());
conn.Open();
qry_select= "select * from [" + filename + "]";
//Creates the data adapter
OdbcDataAdapter oledb_da = new OdbcDataAdapter(qry_select, conn);
//Fills records to the Dataset from CSV file
oledb_da.Fill(ds, "csv_data");
//closes the connection
conn.Close();
gv_csv.DataSource = ds.Tables["csv_data"].DefaultView;
gv_csv.DataBind();
}
catch (Exception e) //Error
{
}
}
See Live Demo
How To Read Data From Csv File in Asp.net ?
How To Read Data From Csv File in using Odbc ?
I am going to show you how to read a CSV File in C# ,Asp.net .Just Follow the following steps.
Step : -1
Create a webform which contains one FileUpload ,Button and a Gridview
<asp:FileUpload ID="fu_csv" runat="server" />
<asp:Button ID="btn_Upload" runat="server" Text="Upload" OnClick="btn_Upload_Click"/>
<asp:GridView ID="gv_csv" runat="server" HeaderStyle-BackColor="BurlyWood"></asp:GridView>
Step :-2
Then Go to Code behind and write the following Code
protected void btn_Upload_Click(object sender, EventArgs e)
{
fu_csv.SaveAs(Server.MapPath("~/upload/") + fu_csv.FileName);
string csv_path = Server.MapPath("~/upload/") + fu_csv.FileName;
ReadCVS( Server.MapPath("~/upload/"),fu_csv.FileName);
}
public void ReadCVS(string dir_path,string filename)
{
DataSet ds = new DataSet();
try
{
// Creates and opens an ODBC connection
string ConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + dir_path + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
string qry_select;
OdbcConnection conn;
conn = new OdbcConnection(ConnString.Trim());
conn.Open();
qry_select= "select * from [" + filename + "]";
//Creates the data adapter
OdbcDataAdapter oledb_da = new OdbcDataAdapter(qry_select, conn);
//Fills records to the Dataset from CSV file
oledb_da.Fill(ds, "csv_data");
//closes the connection
conn.Close();
gv_csv.DataSource = ds.Tables["csv_data"].DefaultView;
gv_csv.DataBind();
}
catch (Exception e) //Error
{
}
}
See Live Demo
No comments:
Post a Comment