I am creating a small application to import excel data into my database, when i click the button it crashes with the error
External table is not in the expected format.
I tried googling and changing the codes here and there but the problem still occurs. I tried saving the file as a .xls and when i run the code the page went offline with google chrome's This webpage is not available (Cannot even enter debugging)
Here is my code:
string strConnection = ConfigurationManager.ConnectionStrings["---"].ConnectionString;
String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0\"", filePath);
//Create Connection to Excel work book
using (OleDbConnection excelConnection = new OleDbConnection(excelConnString))
{
//Create OleDbCommand to fetch data from Excel
using (OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", excelConnection))
{
excelConnection.Open();
using (OleDbDataReader dReader = cmd.ExecuteReader())
{
using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
{
//Give your Destination table name
sqlBulk.DestinationTableName = "TableName";
sqlBulk.WriteToServer(dReader);
}
}
}
}