[ad_1]
I'm trying to work with uploading excel in asp.net c# web form and save in sql however it also upload duplicate data in row. <pre lang="C#">private bool uploadfile1() { try { if (FileUpload1.PostedFile.FileName != "" || FileUpload1.PostedFile.FileName != "File Template") { this.checkDirectory(); FileUpload1.PostedFile.SaveAs(Server.MapPath("") + "\\Uploads\\" + FileUpload1.FileName); filename = FileUpload1.FileName; path = Server.MapPath("") + "\\Uploads\\"; ExcelCS xl = new ExcelCS(); xl.UserId = (Session["userid"] != null) ? Session["userid"].ToString() : String.Empty; xl.Session = (Session["sessionID"] != null) ? Session["sessionID"].ToString() : String.Empty; if (xl.ParseExcelFile(path + filename, true, 1)) { listdata save = new listdata(); if (save.save()) { Class.JSclass.ShowAlert(save.RecordsUploaded.ToString() + " item(s) uploaded and " + save.TotalUpdated.ToString() + " item(s) updated out of " + save.TotalRecords.ToString() + " Record(s)."); listweb.Class.Logger.writeLog(Session["userid"].ToString(), Session["sessionID"].ToString(), "frmUploadFile.uploadfile1", "File uploaded successfully!", "", "", Class.LogCategories.SELECT); Class.JSclass.refreshMainPage(); return true; } } else { Class.JSclass.ShowAlert("Invalid file format please use specified template."); listweb.Class.Logger.writeLog(Session["userid"].ToString(), Session["sessionID"].ToString(), "frmUploadFile.uploadfile1.ParseExcelFile", "Error: " + xl.Message, "", "", Class.LogCategories.SELECT); return false; } } } catch (Exception e) { Class.JSclass.ShowAlert("Error: " + e.Message); listweb.Class.Logger.writeLog(Session["userid"].ToString(), Session["sessionID"].ToString(), "frmUploadFile.uploadfile1", "Error: " + e.Message, "", "", Class.LogCategories.SELECT); } Class.JSclass.ShowAlert("File upload failed!"); return false; }
私が試したこと:
I tried only tried to save the upload in sql however with duplicate/ same data in upload/save in sql. I wanted to void the duplication of the data
解決策 1
重複を避ける 2 つの方法:
1)最初に行が存在するかどうかを確認し、存在する場合はINSERTしないでください
2) UPDATE を実行して、「影響を受ける行」の数がゼロかどうかを確認してください。 そうであれば、そのような行はなく、安全に INSERT できます。 ゼロ以外の場合、新しい値が古い値を上書きしています。
[ad_2]
コメント