Skip Navigation LinksHome > Categories > Code from a Category

How to Import or insert Excel Sheet data and records into SQL Server 2005, 2008, 2012 using ASP.Net



User Name: codelecturer
Name: Mike Chauhan
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 3396
Add Date: 02/03/2013
In this article we will learn about how to import or insert Excelsheet data and records into SQL Server.
Here I am explaining how to import Excel Sheet Rows into SQL Server Database table using ADO.Net in an ASP.Net Web Application.

Concept

1. User uploads the Excel File.
2. Based on the extension it is decided whether it is Excel 97 – 2003 or Excel 2007 format.
3. User can select whether the Excel Sheet has header row or not using the Radio Buttons
4. The Excel file is uploaded and then sheets in the Excel workbook are read into a DropDownList.
5. User has to now select the Sheet from the dropdown whose data he wants to import.
6. User has to enter the name of the table which he wants the data to be imported.
7. User presses OK Button and the data is imported into the SQL Server Database table and the user is updated with the status.

Stored Procedures

For this article I have created two stored procedures one to read the Excel 97 – 2003 format and other Excel 2007 format. Though the Microsoft Ace Driver can read both still I have used Jet for Excel 97 - 2003 formats.

Excel 97 – 2003 Format



Excel 2007 Format



In the above stored procedures, I have used four input parameters

1.@SheetName - Name of the Excel Sheet to be read.
2.@FilePath - Path of the Excel File
3.@HDR - Indicates whether first row in the excel sheet will be considered as Header row or not.
4.@TableName - The name of the table in which the Excel Sheet data will be transferred if the table is not present it will be created.

When you run the above stored procedure first time you might get the following error message.

Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.

In order to run the above stored procedure you will need to enable Surface Area Configuration in your SQL Server in the following way

Execute the below four statements one by one in the SQL Server Query Analyzer


Also in order to use the Microsoft OLEDB Ace Driver you will need to install the 2007 Office System Driver: Data Connectivity Components which is necessary for Microsoft OLEDB ACE 12.0 driver to work using the link below

2007 Office System Driver: Data Connectivity Components

I faced the following Error while running the OLEDB Ace stored procedure

Msg 7399, Level 16, State 1, Line 2
The OLE DB provider "Microsoft.Ace.OLEDB.12.0" for linked server "(null)" reported an error. Access denied.
Msg 7350, Level 16, State 2, Line 2
Cannot get the column information from OLE DB provider "Microsoft.Ace.OLEDB.12.0" for linked server "(null)".

I have written an article on the solutions for it. You can visit it using the link below

The OLE DB provider "Microsoft.Ace.OLEDB.12.0" for linked server "(null)"

Front End Design

Below is the markup of the HTML mark of the asp.net web page. There are 2 panels. First with an upload button, Asp.Net FileUpload control and a label to display the status used to upload the Excel File. Second one with label to display the uploaded Excel file name, DropDownList which contain the names of Sheets of the Excel workbook, an ASP.Net RadioButtonList to capture whether the Sheet has header row and finally two buttons one to import the Excel Sheet rows into the SQL Server Database table and other one to cancel.



Namespaces

You will require to import the following namespaces

C#


VB.Net


Web.Config Configurations

I have used a AppSettings key FolderPath to store the path of the folder where the uploaded excel file will be stored.

There are three connections strings as described below

1. Excel03ConString - Conncection String for Excel 97 – 2003 formats
2. Excel07ConString - Connection String for Excel 2007 format
3. conString - Connection String for the SQL Server 2005 Express Database.



Uploading the excel Workbook

User has to first upload the Excel File whose data he wants to transfer to the SQL Server database onto the server using ASP.Net FileUpload Control and a Upload button. The code snippet for the Upload Button is given below.

C#


VB.Net

The above code snippet simply uploads the Excel Workbook into the path defined in the Web.Config key. The figure below displays the User Interface for uploading the Excel File.



You will notice GetExcelSheets function being called on the click of Upload Button. As the name suggests the function reads the names of all the sheets present in the Excel Workbook and binds the result to DropDownList. The complete function is given below

C#

VB.Net


Importing the rows from the excel sheet to the database table

Once the Sheets are filed in the DropDownList the user interface looks as like below.

As you can see the excel file name is displayed along with all the sheets in the DropDownList. The RadioButtonList captures the information about the header row in the Excel Sheet. Then once the user presses Save button all the rows are read into the database table which the user has entered in the textbox if the table is not present it will be created.



The code snippet for the Save button is give below

C#


VB.Net



The above code snippet simply calls the respective stored procedure based on the extension and the status is displayed to the user as shown in figure below



Happy Programming!!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.