You are on page 1of 6

Import data from Excel to SQL Server | DotNet Finder http://dotnetfinder.wordpress.com/2012/03/20/import-data-from-excel-to-...

‫تعلم الدوت نت بالعربية‬

20 MAR
About these ads (http://en.wordpress.com/about-these-ads/)
in this article I’m going to show you how to import data from Excel to SQL Server in few steps but first take
a look to import/export screen

(http://dotnetfinder.files.wordpress.com/2012/03/image.png)

As you can see the screen is so simple it’s have three controls

FileUpload control to help you to find the location of your Excel file.
Button to import data from Excel and save it in SQL Server.
Label control which is just message to tell of operation result(Success or Fail)

Note: before start reading the steps I want let you to know that I haven’t test this sample either onASP.NET
3.5 or 32-bit MS Office,so may be you do not need the step 1 and step 2.

1. Create an IIS web site


2. Change .Net Framework to 4.0 for Application Pool of this site
3. Create Excel file and make sure that file contain the column name as following image

(http://dotnetfinder.files.wordpress.com/2012/03/image1.png)

4.Create Table in SQL Server and make sure has the same Columns name with appropriate columns data
type.

1 of 6 16/08/2013 1:09 PM
Import data from Excel to SQL Server | DotNet Finder http://dotnetfinder.wordpress.com/2012/03/20/import-data-from-excel-to-...

(http://dotnetfinder.files.wordpress.com/2012/03/image2.png)

The following code snippet will show you how upload file in import data from it and save it to SQL Server.

In C#

2 of 6 16/08/2013 1:09 PM
Import data from Excel to SQL Server | DotNet Finder http://dotnetfinder.wordpress.com/2012/03/20/import-data-from-excel-to-...

1 // if you have Excel 2007 uncomment this line of code
2 //  string excelConnectionString =string.Format("Provider=Microsoft.Jet.OLEDB.4.0
3
4  
5 string ExcelContentType = "application/vnd.ms‐excel";
6 string Excel2010ContentType = "application/vnd.openxmlformats‐officedocument.spre
7 if (FileUpload1.HasFile)
8 {
9 //Check the Content Type of the file
10 if(FileUpload1.PostedFile.ContentType==ExcelContentType || FileUpload1.PostedFile
11 {
12 try
13 {
14 //Save file path
15 string path = string.Concat(Server.MapPath("~/TempFiles/"), FileUpload1.FileName)
16 //Save File as Temp then you can delete it if you want
17 FileUpload1.SaveAs(path);
18 //string path = @"C:\Users\Johnney\Desktop\ExcelData.xls";
19 //For Office Excel 2010  please take a look to the followng link  http://social.m
20 string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;D
21  
22 // Create Connection to Excel Workbook
23
using (OleDbConnection connection =
24
new OleDbConnection(excelConnectionString))
25
{
26
OleDbCommand command = new OleDbCommand
27
("Select * FROM [Sheet1$]", connection);
28
 
29
30 connection.Open();
31  
32 // Create DbDataReader to Data Worksheet
33 using (DbDataReader dr = command.ExecuteReader())
34 {
35  
36
37 // SQL Server Connection String
38 string sqlConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=ExcelDB;I
39  
40 // Bulk Copy to SQL Server
41
42 using (SqlBulkCopy bulkCopy =
43 new SqlBulkCopy(sqlConnectionString))
44 {
45 bulkCopy.DestinationTableName = "Employee";
46 bulkCopy.WriteToServer(dr);
47 Label1.Text = "The data has been exported succefuly from Excel to SQL";
48 }
49 }
50 }
51 }
52  
53 catch (Exception ex)
54 {
Label1.Text = ex.Message;
}

3 of 6 16/08/2013 1:09 PM
Import data from Excel to SQL Server | DotNet Finder http://dotnetfinder.wordpress.com/2012/03/20/import-data-from-excel-to-...

}
}

in VB.NET

1 'if you have Excel 2007 uncomment this line of code
2 '  string excelConnectionString =string.Format("Provider=Microsoft.Jet.OLEDB.4.0;
3 'Define the content type
4 Dim ExcelContentType As String = "application/vnd.ms‐excel"
5 Dim Excel2010ContentType As String = "application/vnd.openxmlformats‐officedocume
6 If FileUpload1.HasFile Then
7 If FileUpload1.PostedFile.ContentType = ExcelContentType Or FileUpload1.PostedFil
8 Try
9 'Save file path
10 Dim path As String = String.Concat(Server.MapPath("~/TempFiles/"), FileUpload1.Fi
11 'Save File as Temp then you can delete it if you want
12 FileUpload1.SaveAs(path)
13 'For Office Excel 2010  please take a look to the followng link  http://social.ms
14 Dim excelConnectionString As String = String.Format("Provider=Microsoft.ACE.OLEDB
15  
16
17 ' Create Connection to Excel Workbook
18 Using connection As New OleDbConnection(excelConnectionString)
19  
20 Dim Command As OleDbCommand = New OleDbCommand("Select * FROM [Sheet1$]", connect
21  
22
connection.Open()
23
 
24
25 'Create DbDataReader to Data Worksheet
26 Using reader As DbDataReader = Command.ExecuteReader()
27  
28 ' SQL Server Connection String
29 Dim sqlConnectionString As String = "Data Source=.\sqlexpress;Initial Catalog=Exc
30  
31
32 ' Bulk Copy to SQL Server
33 Using bulkCopy As New SqlBulkCopy(sqlConnectionString)
34  
35 bulkCopy.DestinationTableName = "Employee"
36 bulkCopy.WriteToServer(reader)
37 Label1.Text = "The data has been exported succefuly from Excel to SQL"
38 End Using
39 End Using
40 End Using
41 Catch ex As Exception
42 Label1.Text = ex.Message
End Try
End If
End If

4 of 6 16/08/2013 1:09 PM
Import data from Excel to SQL Server | DotNet Finder http://dotnetfinder.wordpress.com/2012/03/20/import-data-from-excel-to-...

after following the above steps you can run web and click on the browse then select Excel file then click
import to save Excel file data on SQL Server side.

You can download code from the following link ( Please do not forget to rate it)

http://code.msdn.microsoft.com/Imoprt-Data-from-Excel-to-705ecfcd (http://code.msdn.microsoft.com
/Imoprt-Data-from-Excel-to-705ecfcd)

References

SqlBulkCopy Class (http://msdn.microsoft.com/en-us/library


/system.data.sqlclient.sqlbulkcopy.aspx)
Transferring Data Using SqlBulkCopy (http://www.codeproject.com/Articles/18418/Transferring-
Data-Using-SqlBulkCopy)

I hope you find this sample useful and i will be happy to answer your questions.

Regards.

1 Comment
Posted by Ahmed Naji on March 20, 2012 in ASP.NET, C#, VB.NET

Tags: EXCEL, iis, iis 7, OFFICE, office 2010

daryl eaton

April 26, 2012 at 9:50 pm

thank you so much! This will save me hours. Great job, thanks for sharing.

Reply

5 of 6 16/08/2013 1:09 PM
Import data from Excel to SQL Server | DotNet Finder http://dotnetfinder.wordpress.com/2012/03/20/import-data-from-excel-to-...

Blog at WordPress.com. The Choco Theme.

Entries (RSS) and Comments (RSS)

Follow

Powered by WordPress.com

6 of 6 16/08/2013 1:09 PM

You might also like