将Excel数据上传到sql server C# [英] Upload Excel data into sql server C#

查看:87
本文介绍了将Excel数据上传到sql server C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EXCEL DATA

 Id名称薪资位置
1 Ram 45000 Chennai
2 Krish 60000德里
3 Gokul 80000 Chennai
4 Aditya 100000孟买
5 jai 80000德里
6 karuna 90000 Chennai



这是Excel数据我想用两个表将数据导入到sql server中,其中Place是外键表。我怎么能为此编写代码。

表1

 Id名称薪水地方
1 Ram 45000 1
2 Krish 60000 2
3 Gokul 80000 1
4 Adit 100000 3
5 jai 80000 2
6 karuna 90000 1





Tabel2

 ID地方
1 Chennai
2德里
3孟买





先谢谢

解决方案

尝试

将数据导入SQL Server的简单方法 [ ^ ]

如何将数据从Excel导入到SQL Server [ ^ ]

从Excel导入表 [ ^ ]


一种可能的方式



1)将excel数据导出为文件名'csv'



2)创建表raw_table1



3)使用此 http://dev.mysql.com /doc/refman/5.1/en/load-data.html [ ^ ]将csv数据文件上传到raw_table1表



4)创建'正确'表1 table(如果已经存在则截断/清除它)



5为table2数据创建一个表并将该参考数据插入其中



现在我们得到了好东西



6)wri一个插入查询沿着



插入table1(id,name,salary,place)选择a.id,a.name,a.salary,b .id来自raw_table1 a,table2 b其中a.place = b.place



检查插入的行数与原始table1数据集中的数量 - 如果有的话一个错误,请确保你已经在table2中定义了所有的地方



(有一种更好的方法可以确保行加载,即使你不能匹配'地方')

EXCEL DATA

Id	Name	Salary	Place
1	Ram    	45000	Chennai
2	Krish	60000	Delhi
3	Gokul	80000	Chennai
4	Aditya 100000	Mumbai
5	jai   	80000	Delhi
6	karuna	90000	Chennai


This is Excel Data i want to import data into sql server with two tables,where Place is the foreign key table. How i can Write code for this.
Table1

Id	   Name	Salary	Place
1	Ram     45000	   1
2	Krish	60000	   2
3	Gokul	80000	   1
4	Adit   100000	   3
5	jai     80000	   2
6	karuna	90000	   1



Tabel2

Id Place
1  Chennai
2  Delhi
3  Mumbai



Thanks in Advance

解决方案

Try
Simple way to import data into SQL Server [^]
How to import data from Excel to SQL Server [^]
Import table from Excel[^]


One possible way

1) export the excel data to a filename as 'csv'

2) create a table raw_table1

3) use this http://dev.mysql.com/doc/refman/5.1/en/load-data.html[^] to upload the csv data file to the raw_table1 table

4) create the 'proper' table1 table (or truncate/clear it if it already exists)

5 create a table for the table2 data and insert that reference data into it

now we get to the good stuff

6) write an insert query along the lines of

insert into table1 (id, name, salary, place) select a.id, a.name, a.salary, b.id from raw_table1 a, table2 b where a.place = b.place

check how many rows you get inserted vs the number in the original table1 dataset - if there's an error, make sure you have all the places defined in table2

(there's an even better way of doing this to make sure rows get loaded even if you cant match 'place')


这篇关于将Excel数据上传到sql server C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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