Friday, September 30, 2011

Read CSV Files into Dataset using asp.net

set in web.config
<appSettings>
  <add key="connxls" value="Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='text;HDR=Yes;FMT=Delimited';Data Source=" />
</appSettings>


In test.aspx.cs
using System.Data.OleDb;
using System.Collections.Specialized;



protected void Page_Load(object sender, EventArgs e)
    {
NameValueCollection config = (NameValueCollection)ConfigurationSettings.GetConfig("appSettings");
        ConnCSV = config["connxls"];
      

    }

protected void UploadButton_Click(object sender, EventArgs e)
    {
string str=strSavedFilePath;
System.IO.FileInfo file11 = new FileInfo(str);
            DataSet dsCSV = new DataSet();
            dsCSV = ConnectFile(file11);

}
  private DataSet ConnectFile(FileInfo filetable)
    {
        DataSet ds = new DataSet();
        string str;
        try
        {
            ConnCSV = ConnCSV + filetable.DirectoryName.ToString();
            string sqlSelect;
            OleDbConnection objOleDBConn;
            OleDbDataAdapter objOleDBDa;
            objOleDBConn = new OleDbConnection(ConnCSV);
            objOleDBConn.Open();
            sqlSelect = "select * from [" + filetable.Name.ToString() + "]";
            objOleDBDa = new OleDbDataAdapter(sqlSelect, objOleDBConn);
            objOleDBDa.Fill(ds);
            objOleDBConn.Close();
        }
        catch (Exception ex)
        {
            str = ex.Message;
        }
        return ds;
    }






No comments:

Post a Comment