如何将Excel数据与SQL进行比较并删除重复记录使用Linq [英] How to compare excel data to SQL and remove duplicate record Using Linq
本文介绍了如何将Excel数据与SQL进行比较并删除重复记录使用Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
美好的一天,我几乎没有问题删除重复的记录
首先,我希望能够将excel的记录与SQL的记录进行比较,如果相同,则将删除。 。我如何实现以下目标是如何使用linq导入到sql的代码
尝试
{
string Pat = string 。 Concat(Server.MapPath( 〜/ FileUpload / + FileUpload1.FileName));
FileUpload1.SaveAs(Pat);
string PathToExcelFile = Pat;
var ExcelFile = new ExcelQueryFactory(PathToExcelFile);
ExcelFile.AddMapping<问题>(x = > x.QuestionOrder, QuestionNo);
ExcelFile.AddMapping<问题>(x = > x.Title, 问题跨度>);
ExcelFile.AddMapping<问题>(x = > x.Answer1, A跨度>);
ExcelFile.TrimSpaces = LinqToExcel.Query.TrimSpacesType.Both;
ExcelFile.ReadOnly = true ;
IQueryable<问题> Quest =( from c ExcelFile.Worksheet< Question>()选择 c);
foreach (问题qu 在 Quest中)
{
cdc。 Questions.Add(曲);
cdc.SaveChanges();
lblmess.Text = 成功导入;
}
}
catch (例外情况)
{
lblmess.Text = ex.Message;
}
非常感谢..我真的很感激你的时间和精力。
解决方案
< blockquote>
将Excel数据导入sql表(主表的副本),然后执行与主表的连接以选择匹配的记录。< br />
并在每次上传时截断此Relica表excel文件。
Good Day all, I am having little issue removing duplicate record
First I want to be able to compare the record from excel to that of the SQL, if the same, one will be deleted.. How do i please achieve that below is the code on how to import to sql using linq
try
{
string Pat = string.Concat(Server.MapPath("~/FileUpload/" + FileUpload1.FileName));
FileUpload1.SaveAs(Pat);
string PathToExcelFile = Pat;
var ExcelFile = new ExcelQueryFactory(PathToExcelFile);
ExcelFile.AddMapping<Question>(x => x.QuestionOrder, "QuestionNo");
ExcelFile.AddMapping<Question>(x => x.Title, "Question");
ExcelFile.AddMapping<Question>(x => x.Answer1, "A");
ExcelFile.TrimSpaces = LinqToExcel.Query.TrimSpacesType.Both;
ExcelFile.ReadOnly = true;
IQueryable<Question> Quest = (from c in ExcelFile.Worksheet<Question>() select c);
foreach (Question qu in Quest)
{
cdc.Questions.Add(qu);
cdc.SaveChanges();
lblmess.Text = "Imported Successfully";
}
}
catch(Exception ex)
{
lblmess.Text = ex.Message;
}
Thank you very much.. I really do appreciate your time and effort.
解决方案
Import Excel data to sql table(replica of main table) then perform join with main table to select matched record.<br /> and truncate this Relica table every time when u upload excel file.
这篇关于如何将Excel数据与SQL进行比较并删除重复记录使用Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文