Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
EXCEL DATA
HTML
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
HTML
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
HTML
Id Place
1  Chennai
2  Delhi
3  Mumbai


Thanks in Advance
Posted

 
Share this answer
 
Comments
Prasad Khandekar 26-Nov-14 5:58am    
+5
Abhinav S 26-Nov-14 21:53pm    
Thanks.
Gokul Athithan 27-Nov-14 1:19am    
how to use sqlbulkcopy for this
Gokul Athithan 26-Nov-14 6:05am    
I want to like Table1
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')
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900