如何将Excel数据插入SQL Server表
问题描述:
我有Excel工作表,并且有1000条记录,我想在SQL表中插入之前验证重复的ID和名称,请帮助我.
如果记录存在,那么我将移至下一条记录,然后插入.
I have Excel sheet and have 1000 records are there, i want to verify duplicate id and name, before insertin in SQL Table, Please Help me.
If the record exists, then i will move to the next record and then insert.
Excel column
------------
Emp_Id
Emp_Name
Table Column
-------------
Emp_ID
Emp_Name
Emp_Id应从excel检查重复项,然后插入.如果发现重复,则应检查并插入下一行.
Emp_Id Should check the duplication from the excel and then insert. if duplication founds then it should check and insert the next rows.
How to do this..?
答
假定您已将文件中的数据读入某种类型的集合中,例如List< empdata> ;,其中EmpData是您的对象我会做的(几乎是伪代码):
Assuming you have read the data from the file into some sort of collection say List<empdata>, where EmpData is your object here is what I would do (in almost pseudocode):
List<empdata> emps = ParseExcelData();
foreach(EmpData emp in emps)
{
//if this emp doesn't exist in the DB already, the function would return null
//but if its not null, then it will just skip and move to next record repeating the same process
if (GetEmpDataFromDatabaseById(emp.Emp_ID) == null )
{
SaveEmpData(emp);
}
}</empdata>
您将必须编写自己的ParseExcelData,GetEmpDataFromDatabaseById和SaveEmpData方法.
希望这会有所帮助.
干杯.
You would have to write your own ParseExcelData, GetEmpDataFromDatabaseById and SaveEmpData methods.
Hope this helps.
Cheers.