Você está na página 1de 4

3/21/2014

Import MS Excel data to SQL Server table using C# - CodeProject

Articles Enterprise Systems Office Development Microsoft Excel

Import MS Excel data to SQL Server table using C#


By Musakkhir , 13 Aug 2013
5.00 (3 votes)

Introduction
If you already have data in MS Excel file, and want to migrate your MS Excel data to SQL Server table, follow the below steps: Step 1: Lets take an example to import data to SQL Server table. I am going to import student information data from an MS Excel sheet to the tStudent SQL table: Step 2: Now design a tStudent table in SQL Server
C R E A T ET A B L E ( S T U D E N TV A R C H A R ( 6 4 ) , R O L L N OV A R C H A R ( 1 6 ) , C O U R S EV A R C H A R ( 3 2 ) , )

Your MS Excel sheet and SQL table are ready, now its time to write C# code to import the Excel sheet into the tStudent table. Step 3: Add these two namespaces in your class file:
http://www.codeproject.com/Tips/636719/Import-MS-Excel-data-to-SQL-Server-table-using-Csh?display=Print 1/4

3/21/2014

Import MS Excel data to SQL Server table using C# - CodeProject

U S I N GS Y S T E M . D A T A . O L E D B ; U S I N GS Y S T E M . D A T A . S Q L C L I E N T ;

Step 4: Add below method in your class file, you can call this method from any other class and pass the Excel file path:
p u b l i cv o i di m p o r t d a t a f r o m e x c e l ( s t r i n ge x c e l f i l e p a t h ) { / / d e c l a r ev a r i a b l e s-e d i tt h e s eb a s e do ny o u rp a r t i c u l a rs i t u a t i o n s t r i n gs s q l t a b l e=" t d a t a m i g r a t i o n t a b l e " ; / /m a k es u r ey o u rs h e e tn a m ei sc o r r e c t ,h e r es h e e tn a m ei ss h e e t 1 ,s oy o uc a nc h a n g ey o u rs h e e tn a m ei fh a v e d i f f e r e n t s t r i n gm y e x c e l d a t a q u e r y=" s e l e c ts t u d e n t , r o l l n o , c o u r s ef r o m[ s h e e t 1 $ ] " ; t r y { / / c r e a t eo u rc o n n e c t i o ns t r i n g s s t r i n gs e x c e l c o n n e c t i o n s t r i n g=@ " p r o v i d e r = m i c r o s o f t . j e t . o l e d b . 4 . 0 ; d a t as o u r c e = "+e x c e l f i l e p a t h+ " ; e x t e n d e dp r o p e r t i e s = "+" \ " e x c e l8 . 0 ; h d r = y e s ; \ " " ; s t r i n gs s q l c o n n e c t i o n s t r i n g=" s e r v e r = m y d a t a b a s e s e r v e r n a m e ; u s e r i d = d b u s e r i d ; p a s s w o r d = d b u s e r p a s s w o r d ; d a t a b a s e = d a t a b a s e n a m e ; c o n n e c t i o nr e s e t = f a l s e " ; / / e x e c u t eaq u e r yt oe r a s ea n yp r e v i o u sd a t af r o mo u rd e s t i n a t i o nt a b l e s t r i n gs c l e a r s q l=" d e l e t ef r o m"+s s q l t a b l e ; s q l c o n n e c t i o ns q l c o n n=n e ws q l c o n n e c t i o n ( s s q l c o n n e c t i o n s t r i n g ) ; s q l c o m m a n ds q l c m d=n e ws q l c o m m a n d ( s c l e a r s q l ,s q l c o n n ) ; s q l c o n n . o p e n ( ) ; s q l c m d . e x e c u t e n o n q u e r y ( ) ; s q l c o n n . c l o s e ( ) ; / / s e r i e so fc o m m a n d st ob u l kc o p yd a t af r o mt h ee x c e lf i l ei n t oo u rs q lt a b l e o l e d b c o n n e c t i o no l e d b c o n n=n e wo l e d b c o n n e c t i o n ( s e x c e l c o n n e c t i o n s t r i n g ) ; o l e d b c o m m a n do l e d b c m d=n e wo l e d b c o m m a n d ( m y e x c e l d a t a q u e r y ,o l e d b c o n n ) ; o l e d b c o n n . o p e n ( ) ; o l e d b d a t a r e a d e rd r=o l e d b c m d . e x e c u t e r e a d e r ( ) ; s q l b u l k c o p yb u l k c o p y=n e ws q l b u l k c o p y ( s s q l c o n n e c t i o n s t r i n g ) ; b u l k c o p y . d e s t i n a t i o n t a b l e n a m e=s s q l t a b l e ; w h i l e( d r . r e a d ( ) ) { b u l k c o p y . w r i t e t o s e r v e r ( d r ) ; } o l e d b c o n n . c l o s e ( ) ; } c a t c h( e x c e p t i o ne x ) { / / h a n d l ee x c e p t i o n }

In the above function you have to pass the MS Excel file path as a parameter. If you want to import your data by providing the client access to
http://www.codeproject.com/Tips/636719/Import-MS-Excel-data-to-SQL-Server-table-using-Csh?display=Print 2/4

3/21/2014

Import MS Excel data to SQL Server table using C# - CodeProject

select the Excel file and import, then you might have to use the ASP.NET File control and upload the Excel file on the server in some temp folder, then use the file path of the uploaded Excel file and pass the path in the above function. Once data import is complete then you can delete the temporary file. The above method first deletes the existing data from the destination table, then imports the Excel data into the same table.

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

About the Author


Musakkhir
Software Developer (Senior) India Musakkhir Sayyed is a Software Engineer working in IT Company. He has been a programmer/Software Developer for past 2 years specializing in .NET/C# development. Currently, he is a Silverlight, WPF/Sharepoint developer specializing in BCS Services. Follow on
Twitter Google

Comments and Discussions


http://www.codeproject.com/Tips/636719/Import-MS-Excel-data-to-SQL-Server-table-using-Csh?display=Print 3/4

3/21/2014

Import MS Excel data to SQL Server table using C# - CodeProject

4 messages have been posted for this article Visit http://www.codeproject.com/Tips/636719/Import-MS-Excel-data-to-SQL-Servertable-using-Csh to post and view comments on this article, or click here to get a print view with messages.
Permalink | Advertise | Privacy | Mobile Web03 | 2.8.140315.1 | Last Updated 13 Aug 2013 Article Copyright 2013 by Musakkhir Everything else Copyright CodeProject, 1999-2014 Terms of Use

http://www.codeproject.com/Tips/636719/Import-MS-Excel-data-to-SQL-Server-table-using-Csh?display=Print

4/4

Você também pode gostar