Using this code, I created a datatable out of an Excel document. The data set is also being displayed in a datagridview.
OleDbcon.Open();
tbContainer = new SD.DataTable();
OleDbCommand cmd = new OleDbCommand();
System.Data.DataTable dt = new System.Data.DataTable();
OleDbDataAdapter oda = new OleDbDataAdapter(string.Format("select * from [{0}$]", sheetName), OleDbcon);
DataSet ds = new DataSet();
oda.Fill(ds);
DGVExcel.DataSource = ds.Tables[0];
Console.WriteLine(dt.Rows.Count);
OleDbcon.Close();
Now that I have this Dataset/DataTable, I want to be able to match its data with the contents of an already established access database and then add relevant values.
Say for example I have this in the database:
Name || Fingers || Thumbs || ID
Ross || 10 || 2 || 12
Boss || 9 || 1 || 14
The excel file happens to have the same exact data and the fields will be added except for the ID, which is used to identify the unique entries.
So Ross' value becomes 20||4, Boss becomes 18 || 2.
How do I make use of the DataSet/DataTable I just made to perform this operation?
The ideas I thought of would be to use an 'if not exists' Select * from Table where ID = [?]. But I don't know how to set the ID. Would a for loop work? Pseudocode:
for (int i = 2; i < DB.Count ; i++){
string query = "if not exists Select * from Table where ID = " + DGridView.Rows[i].Cells[LastCell];
I was recommended Linq before but I honestly don't know where to start on that.
EDIT x2:
=======================================
So I think I've been going about this the wrong way, at any rate, I managed to insert my excel data DIRECTLY into the Access table without having to resort to a dataset/raw query. The only caveat is that the table structure needs to match the access table exactly, though I think that was always the case at any rate.
Code:
conn.Open();
string sqls = @"INSERT INTO ACXL SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" + openFileDialog.FileName + "].[" + txtSheetName.Text + "$];";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = sqls;
cmd.ExecuteNonQuery();
conn.Close();
On to the SQL table matching!