1. Add File Upload item toolbox
2. Add submit button to import
3. Add Label to show message after import
4. On button click event insert below code
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
//OleDbConnection oleDBcon = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source" + path + ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";");
OleDbConnection oleDBcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"");
OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]",oleDBcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
oleDBcon.Open();
DbDataReader dr = cmd.ExecuteReader();
string con_str = @"Data Source=.;Initial Catalog=Posts;Integrated security=True";
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "EmployeeDetails";
bulkInsert.WriteToServer(dr);
oleDBcon.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))),File.Delete);
Label1.ForeColor = Color.Green;
Label1.Text = "Inserted";
}
else
{
Label1.ForeColor = Color.Red;
Label1.Text = "Please insert";
}
}
Also include below Dlls:
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Drawing;
using System.Data.Common;
using System;
2. Add submit button to import
3. Add Label to show message after import
4. On button click event insert below code
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
//OleDbConnection oleDBcon = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source" + path + ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";");
OleDbConnection oleDBcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"");
OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]",oleDBcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
oleDBcon.Open();
DbDataReader dr = cmd.ExecuteReader();
string con_str = @"Data Source=.;Initial Catalog=Posts;Integrated security=True";
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "EmployeeDetails";
bulkInsert.WriteToServer(dr);
oleDBcon.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))),File.Delete);
Label1.ForeColor = Color.Green;
Label1.Text = "Inserted";
}
else
{
Label1.ForeColor = Color.Red;
Label1.Text = "Please insert";
}
}
Also include below Dlls:
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Drawing;
using System.Data.Common;
using System;
No comments:
Post a Comment