如何将Excel数据插入SQL Server表 [英] How to insert excel data into SQL Server table

查看:443
本文介绍了如何将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.


这篇关于如何将Excel数据插入SQL Server表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆