Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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 : enter image description here

Database Table:

enter image description here

excel file:- asdf.xls enter image description here

Can Anyone help me with this?It will be Great Help.

share|improve this question

marked as duplicate by podiluska Jun 14 at 21:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Interesting question, let me know if my answer helps you. thanks –  Mzn Jun 14 at 17:06
add comment

1 Answer

My initial guess is that you are not using the correct connection string for the version of Excel sheet you have. This is my guess because you are not even doing anything when the error occurs except attempt to connect to the sheet.

Try to search for the correct connection string.

EDIT:

Try Excel 8.0; instead of Excel 12.0?

share|improve this answer
    
Since it's an xls, I think your edit is correct. –  Doug Glancy Jun 14 at 18:15
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.