My scenario is that I have an Excel Spreadsheet that I want to upload to an SQL Database and UPDATE the information based upon the primary key value (which is locked and hidden within the Excel Spreadsheet). I have the below code that can be used to insert new entries into the database, but im not sure how to adapt it to UPDATE:
string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
OleDbConnection OleDbcon =
new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path +
";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";");
OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]",
OleDbcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
OleDbcon.Open();
DbDataReader dr = cmd.ExecuteReader();
string con_str =
@"Data Source=****************mydatasource""""""""""";
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "TableName";
bulkInsert.WriteToServer(dr);
OleDbcon.Close();
Example Data:
ID StartDate EndDate OrderNumber
1 01/02/2015 NULL 100
2 02/02/2015 NULL 100
3 03/02/2015 NULL 101
4 04/02/2015 NULL 102
5 05/02/2015 NULL 103
- When the data in the database is inserted, the EndDate is Null.
- The End Date is added into the excel sheet, and then I want the C# ASP.Net Application to UPDATE this information and update the SQL Table, not insert it as a new row.
End Data
ID StartDate EndDate OrderNumber
1 01/02/2015 02/02/2015 100
2 02/02/2015 03/02/2015 100
3 03/02/2015 04/02/2015 101
4 04/02/2015 05/02/2015 102
5 05/02/2015 06/02/2015 103
thanks for any help you can give me