52712

OleDB selecting multiple CSVs with first row as field names in C#

So my code looks like this currently, which is all fine and good

String q = "SELECT * FROM "+"test.csv"; try { OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\;Extended Properties=\"Text;HDR=No;FMT=Delimited\""); OleDbDataAdapter da = new OleDbDataAdapter(); DataSet ds = new DataSet(); OleDbCommand cd = new OleDbCommand(q, cn); cn.Open(); da.SelectCommand = cd; ds.Clear(); da.Fill(ds, "CSV"); dataGridView1.DataSource = ds.Tables[0]; cn.Close(); } catch (Exception ex) { MessageBox.Show("There was an issue: " + ex.Message); }

However, I would like to take the first row in my CSV and use that as the field names, so I can do something like

"select a.ID, a.SOMETHING_ELSE from test.csv a" where the CSV looks something like ID,SOMETHING_ELSE,DONT_WANT_THIS 1,blah,I don't want to see this

Yet, from what I see, about all examples on the web show nothing other than select *, and I would like my datagrid to show the headers as the field names selected, instead of F1, F2....

Is this implemented anywhere?

Answer1:

Change your connection string to include HDR=Yes (instead of HDR=No):

OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\;Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"");

For OleDB, the HDR property in the connection string indicates whether or not the first row contains the column names.

Recommend

  • Binding excel file to datagridview
  • Does ADO.NET + massive INSERTs + Excel + C# = “A bad time”?
  • query (join) external wokbooks from a macro
  • Whenever i try to run this code it gives me an error. [Asp.net SQL]
  • Query is not returning results in MS Access with C#?
  • Reading CSV into using OLEDB
  • C# and read values from an Excel file
  • Read Access File into a DataSet
  • read.table function in R drops elements [duplicate]
  • Script task to upload zip file to blob storage with azure datafactory SSIS
  • Convert Json to List with .NET
  • Unable to read xlsx file through OleDb with the size more than ~1mb
  • gridview on page won't refresh, even when calling databind again
  • Reading data from a SAS data source in .Net
  • Why would you want to use composition in golang?
  • git-svn: automatically importing/creating svn revisions as git tags
  • Call objective C macro from swift
  • Difference between two particular dates [duplicate]
  • DataGrid show an empty row when DataTable is empty
  • How to sort a javascript array by date
  • Query with in a loop getting slower and slower
  • ASP.NET Gridview Paging Problem
  • android.app.PendingIntent cannot be accessed ouside the package
  • Exporting SAS DataSet on to UNIX as a text file…with delimiter '~|~'
  • Is the Go HTTP handler goroutine expected to exit immediately in this case?
  • Stacked Bar Chart with percentage composition inside the Bar and total above the Bar in JFreeChart
  • How to remove comma or any characters from Python dataframe column name
  • SSH in Bash Script Messing Up File Read
  • Can you use DataSet and DataTables in a Portable Class Library
  • Javascript, Regex - I need to grab each section of a string contained in brackets
  • How to return DataSet (xsd) in WCF
  • Oledb connection string for excel files
  • Textfile Structure (tables)
  • MySQL WHERE-condition in procedure ignored
  • Microsoft Visual Studio Community 2015 always crashes in Windows 10 if swithed to Visual FoxPro
  • Web-crawler for facebook in python
  • json Serialization in asp
  • trying to dynamically update Highchart column chart but series undefined
  • Can't mass-assign protected attributes when import data from csv file
  • java string with new operator and a literal