Subhash Sharma

Subhash Sharma
Subhash Sharma

This is Subhash Sharma(Software Engineer) Blog

Welcome to this blog and find every solution.............

Search This Blog

Software Engineer(Subhash Sharma)

Software Engineer(Subhash Sharma)
Software Engineer

Monday, February 28, 2011

Read Data from Excel and Export to Sql server

SqlBulkCopy Use for Insert Bulk Data into sql server.You have to create one table inside the sql server and create only structure of the table which we have in Excel .and Use the code Below.

//File upload pick the file name and Details is a sheet name Inside the .Xls file

DataTable dt = new DataTable();

protected void Button2_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+FileUpload1.PostedFile.FileName +";Extended Properties=Excel 8.0; ");
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Details$] ", con);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
InsertBulkDATA();

}
private void InsertBulkDATA()
{
SqlBulkCopy sbc = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["kumarConnectionString"].ConnectionString);
sbc.DestinationTableName = "tblData";
sbc.WriteToServer(dt);
}