ARTICLE

Uploading XL Sheet data into SQL Server 2005 using C#

Posted by Srinivas Articles | ASP.NET Programming May 19, 2009
Uploading XL Sheet data into SQL Server 2005 using C#
Reader Level:


Uploading XL Sheet data into SQL Server 2005 using C#:

This example will explain you how to upload the XL Sheet data into SQL Server 2005 using C#.

Add on upload control and button control to the ASP.NET 2.0 webform.

In button click event paste the following code:

a3.bmp

protected void btn_upload1_Click(object sender, EventArgs e)

    {

        try

        {

            string FilePath = System.IO.Path.GetFullPath(fileload1.PostedFile.FileName);

            string xConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";" + "Extended Properties=Excel 8.0;";

            using (OleDbConnection connection = new OleDbConnection(xConnStr))

            {

                OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", connection);

                connection.Open();

                using (DbDataReader dr = command.ExecuteReader())

                {

                    string conStr = ConfigurationManager.AppSettings["conStr"];

                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conStr))

                    {

                        bulkCopy.DestinationTableName = "Your_Table_Name";

                        bulkCopy.WriteToServer(dr);

                        lbl_Hardwaremsg.Visible = true;

                        lbl_Hardwaremsg.Text = "Hardware Details uploaded successfully";

                    }

                }

            }

      }


"Your_Table_Name" is your table name.

This is working in my application.

Login to add your contents and source code to this article
post comment
     

I am sweetu;
thanks for this code

Posted by Ranjita Mhatre Aug 26, 2010

Nice one

Posted by Easwaran Paramasivam Aug 11, 2010

I am not used Microsoft jet to connect to MS SQL Sever. i am used this Microsoft jet to connect to MS XL sheet.

This is the Query used to send the Data from XL Sheet to Database. I got this query in Microsoft site.

INSERT INTO emp SELECT * from OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\text.xls','SELECT * FROM [sheet1$]')

Please send your comments to Microsoft.

This is not the way of giving the reply on somebodys article.

Posted by Srinivas Jun 19, 2009

Good Job this kind of operation does not work with formatted excel sheet file because you have merged cells images and other staff.

so u have to use special controls or using office automation to extract data from various cells that for sure not in the first row

Posted by Mohammed Jun 02, 2009

I've never seen anyone call an SQL Server using Microsoft.Jet.


But seriously: Should the title say "Microsoft Access" or should the code be modified to access SQL Server?

I am hesitant to consider the validity of the code when I see errors this quick in this short of an example.

Posted by Joe Pool May 29, 2009
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.