I am trying to upload my excel 2013 file contents to sql server. Here is my code :
At line
excelConnection.Open();
I am getting an Exception : External table is not in the expected format.
I referred to this solution
and followed the exact steps of the accepted solution, yet I am getting the same exception.
I even removed the header in my excel file, yet the issue persisted.
This is my excel
Ans this is my database table
C#:
try
{
string connectionString = @"data source=DELL\SQLEXPRESS01 ; initial catalog = amen; persist security info = True; Integrated Security = SSPI; ";
SqlConnection SQLConnection = new SqlConnection(connectionString);
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(@"C:\Users\AMEN\Desktop\" + filename);
string filepath = @"C:\Users\AMEN\Desktop\" + filename;
String excelConnString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml\"", filepath);
//Create Connection to Excel work book
SQLConnection.Open();
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(SQLConnection))
{
//Give your Destination table name
sqlBulk.DestinationTableName = "employee";
sqlBulk.WriteToServer(dReader);
}
}
}
}
SQLConnection.Close();
Label1.Text = "Your file uploaded successfully";
Label1.ForeColor = System.Drawing.Color.Green;
}
catch
{
Label1.Text = "Your file not uploaded";
Label1.ForeColor = System.Drawing.Color.Red;
}
At line
excelConnection.Open();
I am getting an Exception : External table is not in the expected format.
I referred to this solution
External table is not in the expected format.
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
stackoverflow.com
and followed the exact steps of the accepted solution, yet I am getting the same exception.
I even removed the header in my excel file, yet the issue persisted.
This is my excel
Ans this is my database table
C#:
create table employee( empno int, empname varchar(30), salary decimal(5,2), ismanager bit)
select * from employee