Hi UdhayaKumar,
Here is the code
public void ExportToSql(string filename,string savepath)
{
string connectionString = "";
savepath = savepath + "\\" + filename;
if (System.IO.Path.GetExtension(filename) == ".xls")
{
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + savepath + ";Extended Properties=Excel 8.0";
}
else if (System.IO.Path.GetExtension(filename) == ".xlsx")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +savepath + "; Extended Properties=Excel 12.0;";
}
using (OleDbConnection con = new OleDbConnection(connectionString ))
{
con.Open();
OleDbCommand com = new OleDbCommand("Select * from [Sheet1$]", con);
OleDbDataReader dr = com.ExecuteReader();
using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["OnlineExamConnectionString"].ConnectionString))
{
sqlcon.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlcon))
{
bulkCopy.DestinationTableName = "tblCollege";
bulkCopy.WriteToServer(dr);
}
}
dr.Close();
dr.Dispose();
}
}
Reference :
[
^]
http://www.c-sharpcorner.com/UploadFile/99bb20/import-excel-data-to-sql-server-in-Asp-Net/[
^]
If you are fine with the solution ,please rate the solution provided :)
Regards,
Praveen Nelge