-1

I am trying to import data from Excel into SQL Server 2008 R2. Using the OLEDB provider. I am using Visual Studio 2010. ACE.OLEDB provider is not registered with my Visual Studio. I have 100,000 records in my Excel but it is copying only 65,535. The following is my code:

namespace WebApplication1
{
public partial class Upload_page : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        Response.Write("Working");
        string path = "path";
        OleDbConnection connection = null;
        if (FileUpload1.HasFile)
        {
            path = string.Concat(Server.MapPath("~/Uploaded Folder/" + FileUpload1.FileName));
            FileUpload1.SaveAs(path);
        }
        path = "C:\\Users\\temp\\Documents\\visual studio 2010\\Projects\\WebApplication1\\WebApplication1\\Uploaded Folder\\TempReport.xls";
        try
        {
            string excelConnectionString = string.Format("Provider=Microsoft.jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0", path);
            connection = new OleDbConnection();
            connection.ConnectionString = excelConnectionString;
            OleDbCommand command = new OleDbCommand("select * from [ISO$]", connection);
            connection.Open();
            // Create DbDataReader to Data Worksheet
            DbDataReader dr = command.ExecuteReader();
            string sqlConnectionString = @"Initial Catalog=CensusTrack;Data Source=CWVQ-2K12E1D;Integrated Security=SSPI;";

            // Bulk Copy to SQL Server 
            SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString);
            bulkInsert.BulkCopyTimeout = 0;
            bulkInsert.BatchSize =0;
            bulkInsert.DestinationTableName = "[dbo].[2015_ISO]";
            bulkInsert.WriteToServer(dr);
            connection.Close();
            Response.Write("Completed");
            //Label1.Text = "Ho Gaya";
        }

        catch (Exception obj)
        {
            connection.Close();

        }
    }

}
}

Can any one help me so that I can copy all the data from the Excel to the database table in one go?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Swapna
  • 27
  • 6

1 Answers1

0

You can directly use SQL Mgmt studio for importing excel sheets.

Right click Select Tasks -> Import Data

Please see https://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/

Also see this question Import Excel spreadsheet columns into SQL Server database

Community
  • 1
  • 1
Amitd
  • 4,769
  • 8
  • 56
  • 82