Friday, October 11, 2013

Working code for upload data from Excel to Sql-Server Using C#

Working code for  upload data from Excel 2010 to SQL-Server 2008 , working code.
C# , ASP.Net 4.0


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Common;
using System.Configuration;

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


        string ExcelContentType = "application/vnd.ms-excel";
        string Excel2010ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        if (FileUpload1.HasFile)
        {
            //Check the Content Type of the file
            if (FileUpload1.PostedFile.ContentType == ExcelContentType || FileUpload1.PostedFile.ContentType == Excel2010ContentType)
            {
                try
                {
                    //Save file path
                    string path = string.Concat(Server.MapPath("~/File/"), FileUpload1.FileName);//Create this folder ib your project
                    //Save File as Temp then you can delete it if you want
                    FileUpload1.SaveAs(path);
                    
                    string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);

                    // Create Connection to Excel Workbook
                    using (OleDbConnection connection =  new OleDbConnection(excelConnectionString))
                    {
                        OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", connection);

                        connection.Open();

                        // Create DbDataReader to Data Worksheet
                        using (DbDataReader dr = command.ExecuteReader())
                        {

                            string sqlConnectionString = ConnectionString;

                            // Bulk Copy to SQL Server
                            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                            {
                                bulkCopy.DestinationTableName = "Tbl_YourTable";
                                bulkCopy.WriteToServer(dr);
                                Label5.Text = "The data has been exported successfully from Excel to SQL";
                            }
                        }
                    }
                }

                catch (Exception ex)
                {
                    Label5.Text = ex.Message;
                }
            }
        }
    }
    private string ConnectionString
    {
        get
        {
            string connectionString = ConfigurationManager.ConnectionStrings["connmy"].ConnectionString;
            return connectionString;
        }
    }

}

1 comment:

Anonymous said...

Interesting ... check out Excel apps for E1 here: http://youtube.com/user/aelliuslynx

Also has videos of native .Net integration with E1.