c# - Import CSV into a DataTable -


possible duplicate:
how read csv file .net datatable

i have problem in project, trying read data in csv file, want convert data datatable.

how can this?

my code:

system.data.odbc.odbcconnection conn; datatable insdatatable = new datatable(); system.data.odbc.odbcdataadapter da; string folder = files.fullname; string file = system.io.path.getfilename(fupload.postedfile.filename); conn = new system.data.odbc.odbcconnection(@"driver={microsoft text driver (*.txt; *.csv)};dbq=" + folder + ";extensions=asc,csv,tab,txt;persist security info=false"); da = new system.data.odbc.odbcdataadapter("select * [" + file + "]", conn); da.fill(insdatatable); 

it gives error :

error [42s02] [microsoft][odbc text driver] microsoft jet database engine not find object 'test.csv'. make sure object exists , spell name , path name correctly.

i checking there file 'test.csv' , file path correct :(

couple of comments.

1) may find filehelpers library useful performing operations this. http://www.filehelpers.com/quick_start.html

2) may find following functions useful looking for.

private function gettextdatasource(byval filename string, byval bispreview boolean, byval biscommaseperated boolean) dataview      dim sconnectionstring string = "provider=microsoft.jet.oledb.4.0;data source=" & server.mappath("/folder/path/") & ";"     if chkuploadfilecolumnsfirstrow.checked         sconnectionstring &= "extended properties='text;hdr=yes;fmt=" & iif(biscommaseperated, "csvdelimited", "tabdelimited") & ";imex=1'"     else         sconnectionstring &= "extended properties='text;fmt=" & iif(biscommaseperated, "csvdelimited", "tabdelimited") & ";imex=1'"     end if      dim objconnection new oledbconnection(sconnectionstring)      dim dv dataview     try         dv = getoledbdataview("select " & iif(bispreview, "top 1 ", "") & " * [" & replace(filename, ";", "") & "]", objconnection)     catch ex oledbexception         logger.error(ex.message)         adderror("error selecting data uploaded file, please ensure file matches correct specification , try again.")         return nothing     end try     return dv end function  private function getoledbdataview(byval selectcommand string, byval objconn oledbconnection) dataview     ' create new oledbcommand return data worksheet.     dim objcmdselect new oledbcommand(selectcommand, objconn)      ' create new oledbdataadapter used build dataset      ' based on preceding sql select statement.     dim objadapter1 new oledbdataadapter()      ' pass select command adapter.     objadapter1.selectcommand = objcmdselect      ' create new dataset hold information worksheet.     dim objdataset1 new dataset()      ' fill dataset witht information worksheet.     objadapter1.fill(objdataset1, "excelreturndata")      return objdataset1.tables(0).defaultview end function 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -