I have a dataset that is dynamically generated from a csv file. What I want to do is insert rows into my MS Access table, but I cannot figure out where to start.
The data headers in the data set may vary depending on the order, but the name of the header will always match the access database. Should I statically invoke the header name in the insert command, or can I build the headers from the dataset?
I know how to create a connection and open it in the database, but I'm not sure how to create an insert command to dynamically pull table headers.
I am very green when it comes to C # programming, so if you can write this for me, I would really appreciate it!
Here is an example of access table headers:
Identifier, product, cost, retail
Then a CSV that populates the dataset table. It may have Retail, or it may not be:
Item, cost
Here is the code that I have, but it is not written to the access table. If I believe dtAccess, it will display correctly.
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"C:\\Database.accdb\";Persist Security Info=False;"); myConnection.Open(); string queryString = "SELECT * from " + lblTable.Text; OleDbDataAdapter adapter = new OleDbDataAdapter(queryString, myConnection); DataTable dtAccess = new DataTable(); DataTable dtCSV = new DataTable(); dtCSV = ds.Tables[0]; using (new OleDbCommandBuilder(adapter)) { adapter.Fill(dtAccess); dtAccess.Merge(dtCSV); adapter.Update(dtAccess); } myConnection.Close();
source share