What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 22520 |  Welcome, Guest!   Register  Login
 Home > Blogs > C# > Import Data into Sql Server From Excel Spreadsheet using SQLBulkCopy class ...
Hmanjarawala

Import Data into Sql Server From Excel Spreadsheet using SQLBulkCopy class

 Blog author: Hmanjarawala | Posted on: 8/2/2011 | Category: C# Blogs | Views: 8040 | Status: [Member] | Points: 75 | Alert Moderator   


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;

using System.Data.Common;

using System.Data.SqlClient;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            // Connection String to Excel Workbook

            string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Book1.xls;ExtendedProperties=""Excel 8.0;HDR=YES;"";IMEX=1;";

 

            // Create Connection to Excel Workbook

            using (OleDbConnection connection = new OleDbConnection(excelConnectionString))

            {

                OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Data$]", connection);

 

                connection.Open();

 

                // Create DbDataReader to Data Worksheet

                using (DbDataReader dr = command.ExecuteReader())

                {

                    // SQL Server Connection String

                    string sqlConnectionString = "Data Source=.;Initial Catalog=Test;Integrated Security=True";

 

                    // Bulk Copy to SQL Server

                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))

                    {

                        bulkCopy.DestinationTableName = "ExcelData";

                        bulkCopy.WriteToServer(dr);

                    }

                }

            }

 

        }

    }

}




Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/
Found interesting? Add this to:



 More Blogs from Hmanjarawala

 More ...

Experience:7 year(s)
Home page:http://himanshumbri.blogspot.com
Member since:Saturday, July 30, 2011
Level:Bronze
Status: [Member]
Biography:I am Himanshu Manjarawala, Graduate in Computer Science and MCA From Veer Narmad South Gujarat University, Surat Gujarat India. Currently working as Sr. Software Developer in Automation Anywhere.

 Responses

SheoNarayan
Posted by: SheoNarayan | Posted on: 8/2/2011 | Level: HonoraryPlatinum | Status: [Microsoft_MVP] [Administrator] | Points: 15 | Alert Moderator 

Good codesnippets Himanshu.

Little explanation of the code would have made it awesome!


Regards,
Sheo Narayan, Microsoft MVP
The Founder
http://www.dotnetfunda.com

Kunalverma31
Posted by: Kunalverma31 | Posted on: 8/12/2011 | Level: Starter | Status: [Member] | Points: 15 | Alert Moderator 

I will try to explain you the code snippet

1) string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Book1.xls;ExtendedProperties=""Excel 8.0;HDR=YES;"";IMEX=1;";

above connection string to connect to excel and fetch the data

2)
OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Data$]", connection);
this gets the data from excel tabel [data$], that is already defined in excel.

3)

SqlBulkCopy object is used to insert datatable in database table, please use below URL for more info

http://www.dotnetcurry.com/ShowArticle.aspx?ID=323

i hope this helps u understand the code snippet


>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 4:56:53 AM