Friday, May 7, 2010

Read CSV with oledb to dataset - datatable

string csvDirectory = Server.MapPath("~/media/");
        string CsvFile = "SampleDirectory.csv";
        DataSet ds = new DataSet();
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + csvDirectory + ";Extended Properties='text;HDR=Yes;FMT=Delimited'");
        OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter("SELECT * FROM " + CsvFile, con);
        con.Open();
        ExcelAdapter.Fill(ds);
        con.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();

No comments:

Post a Comment