0
我想創建一個網頁,它有瀏覽Excel文件並將其上傳到數據庫的選項。但是如果我對同一個excel進行一些更改並重新上傳。它應該更新數據庫中的現有數據。但實際上發生了什麼,它被重新插入。導入和更新Excel服務器中的excel數據(無法更新)
這是我的代碼
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string con_str = "Data Source=BG1WS0154\\SQLEXPRESS;Initial Catalog=studentdetails;Integrated Security=True";
string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", OleDbcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
OleDbcon.Open();
DbDataReader dr = cmd.ExecuteReader();
// Bulk Copy to SQL Server
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "tbl_studentdetails";
bulkInsert.WriteToServer(dr);
OleDbcon.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))), File.Delete);
Label1.ForeColor = Color.Green;
Label1.Text = "successfully inserted";
}
else
{
Label1.ForeColor = Color.Red;
Label1.Text = "Please select the File";
}
}