1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Import .csv file using C#

Discussion in 'C#' started by empyrean, Oct 4, 2010.

  1. empyrean

    empyrean New Member

    Joined:
    Oct 4, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    helo all

    i wanted to import .csv file to my application
    Code:
       private DataSet ReadExcelData(string sFilePath)
            {
               
                DataSet dsOutput = new DataSet();
                string sConnection = string.Empty;
                try
                {
                if (TypeExcel == "Excel 12.0 Xml")
                {
    
                    sConnection = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
                }
                else if (TypeExcel == "Excel 8.0") 
                {
                 sConnection = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFilePath + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
                }
                    
                else
                {
                    [B]sConnection = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFilePath + ";Extended Properties=\"ext;HDR=YES;FMT=CSVDelimited\"";[/B]
                 }
                OleDbConnection dbCon = new OleDbConnection(sConnection);
                    dbCon.Open();
                    // Get All Sheets Name
                
                    DataTable dtSheetName = dbCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                
            
    
                // Retrive the Data by Sheetwise
    
                
                    for (int nCount = 0; nCount < 1; nCount++)
                    {
    
                        string sSheetName =   dtSheetName.Rows[nCount]["TABLE_NAME"].ToString();
    
                        string sQuery = "Select * From [" + sSheetName + "]";
    
                        OleDbCommand dbCmd = new OleDbCommand(sQuery, dbCon);
    
                        OleDbDataAdapter dbDa = new OleDbDataAdapter(dbCmd);
                        dbDa.Fill(dtData);
                        dsOutput.Tables.Add(dtData.Copy());
           }
           dbCon.Close();
                 return dsOutput;
      }
    i am getting the following error while using the above code while importing .csv file.. the bold line is the connection string where the error is throwing..
    base {System.Data.Common.DbException} = {"'C:\\ABCD Calif, CA.CSV' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides."}
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    Change that error line to:-

    Code:
    sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
    + "Data Source=\"" + dir + "\\\";"
    + "Extended Properties=\"text;HDR=YES;FMT=Delimited\"";
    
    
    
    
     

Share This Page