Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to read CSV file

How to read CSV file

Scheduled Pinned Locked Moved C#
helptutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kaushik_dass
    wrote on last edited by
    #1

    HOw i Display CSV File Data TO DataGridView Please Help me With Connection string And Code.

    K J 2 Replies Last reply
    0
    • K kaushik_dass

      HOw i Display CSV File Data TO DataGridView Please Help me With Connection string And Code.

      K Offline
      K Offline
      King Julien
      wrote on last edited by
      #2

      Hi!!! Try to do some research by yourself instead of asking code from others.... have a look at this site.... csv to datatable[^] Can give you some insight regarding the issue....

      Have a Happy Coding.....

      K 1 Reply Last reply
      0
      • K King Julien

        Hi!!! Try to do some research by yourself instead of asking code from others.... have a look at this site.... csv to datatable[^] Can give you some insight regarding the issue....

        Have a Happy Coding.....

        K Offline
        K Offline
        kaushik_dass
        wrote on last edited by
        #3

        I have tried it But i got Error Path is not correct even i give it to right path here is my code string Con_Str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My\a.csv;Extended Properties=Text;HDR=No;FMT=Delimited"; OleDbConnection cn = new OleDbConnection(Con_Str); cmd=new OleDbCommand("select * from Book1.CSV",cn ); da=new OleDbDataAdapter(cmd); ds=new DataSet(); da.Fill(ds,"ex"); dataGridView1.DataSource=ds.Tables[0].DefaultView;

        D 1 Reply Last reply
        0
        • K kaushik_dass

          HOw i Display CSV File Data TO DataGridView Please Help me With Connection string And Code.

          J Offline
          J Offline
          Jack Li
          wrote on last edited by
          #4

          using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection; using System.Runtime.InteropServices; using Excel = Microsoft.Office.Interop.Excel; using System.Data.OleDb; using System.Security.Cryptography; public DataSet ExcelToDS(string Path) { //return one dataset object missing = Missing.Value; Excel.Application excelApp = new Excel.Application(); excelApp.Visible = false; try { excelApp.Workbooks.Open(path, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); excelApp.Workbooks[1].Worksheets[i + 1]; Excel.Worksheet ws = (Excel.Worksheet)excelApp.Workbooks[1].Worksheets[1]; string sheetName = ws.Name; string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; DataSet ds = null; strExcel = "select * from [" + sheetName + "$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds); conn.Close(); excelApp.Workbooks.Close(); excelApp.Quit(); excelApp = null; return ds; } catch { MessageBox.Show("Not close EXCEL", "Err Info", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); KillProcess("EXCEL"); return null; } }

          modified on Tuesday, May 5, 2009 3:52 AM

          1 Reply Last reply
          0
          • K kaushik_dass

            I have tried it But i got Error Path is not correct even i give it to right path here is my code string Con_Str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My\a.csv;Extended Properties=Text;HDR=No;FMT=Delimited"; OleDbConnection cn = new OleDbConnection(Con_Str); cmd=new OleDbCommand("select * from Book1.CSV",cn ); da=new OleDbDataAdapter(cmd); ds=new DataSet(); da.Fill(ds,"ex"); dataGridView1.DataSource=ds.Tables[0].DefaultView;

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            So what's the filename of your CSV file?? In your code you have, in the connection string, C:\My\a.csv. But, in the OleDbCommand, you have Book1.CSV. So, which is it??

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups