I am trying to save the data in sql database from an xls format file.i have tried the below code but it shows error that External table is not in the expected format.can anyone help me how i can save the data from an xls format file into database?
code:
protected void Button1_Click(object sender, EventArgs e)
{
string sheet1 = "asdf";
string path = MapPath("~/dataWorldcup/asdf.xls");
OleDbConnection oconn1 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
OleDbCommand ocmd = new OleDbCommand("select * from [" + sheet1 + "$]", oconn1);
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["consed"].ConnectionString);
oconn1.Open();
sqlcon.Open();
OleDbDataReader odr = ocmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(sqlcon);
sqlBulk.DestinationTableName = "[Table]";
sqlBulk.WriteToServer(odr);
sqlcon.Close();
oconn1.Close();
}
Error :
Database Table:
excel file:- asdf.xls
Can Anyone help me with this?It will be Great Help.