Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I trying to load the excel file data into sql server table using the following code
but it showing the following error....
"The Microsoft Jet database engine could not find the object 'order_form'.  Make sure the object exists and that you spell its name and the path name correctly."
Here i am using "order_form" as excel sheet name,"order" as excel book name
location "d:\"
C#
con = new SqlConnection(str);
         
            string ssqltable = "order_item_detl_temp";   
    string myexceldataquery = "select sale_code,sizecd,shade,units from [order_form]";   
   string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data 
 source=d:\order.xls;extended properties=" + "\"excel 8.0;hdr=yes;\"";   

    string sclearsql = "delete from " + ssqltable;   
        SqlCommand cmd = new SqlCommand(sclearsql, con);   
        con.Open();   
        cmd.ExecuteNonQuery();   
        con.Close();   
        OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);   
        OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);   
        oledbconn.Open();   
        OleDbDataReader dr = oledbcmd.ExecuteReader();   
        SqlBulkCopy bulkcopy = new SqlBulkCopy(str);   
        bulkcopy.DestinationTableName = ssqltable;   
        while (dr.Read())   
        {   
            bulkcopy.WriteToServer(dr);   
        }   
        dr.Close();   
        oledbconn.Close();
        MessageBox.Show("Excel data transferred successfully");
Thanks and regards

Madhu

What I have tried:

I have changed the excel file path location and path format like ('/','\','//' ) etc.. in datasource of sexcelconnectionstring. but it is not working.
Posted
Updated 4-Sep-18 3:14am
v2

1 solution

Changing the name of the file is not likely to make any difference. You need to check the worksheet names as found by OleDb.

You can list the names with the following code:
C#
DataTable dtSchema = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
// Use LINQ to get the table (i.e. sheet) names from the spreadsheet schema
List<object> worksheets = (from dr in dtSchema.AsEnumerable()	// returns an IEnumerable<DataRow> collection
select dr["TABLE_NAME"]).ToList();    // gets the sheet name from each schema entry
foreach (string name in worksheets)
{
    // list or show the names
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900