简体   繁体   中英

The Microsoft Office Access database engine could not find the object 'PPR_Status_Detailed'

database design

I am trying to upload excel spreadsheet to sql database through c# asp.net web page. I keep getting the error: "The Microsoft Office Access database engine could not find the object 'PPR_Status_Detailed', Make sure the object exists and that you spell its name and the path name correctly." I dont understand why , i have changed my connection string and sheet name as but still get this error . when i open the excel file and then try to upload it , it says "External Table not in expected format" . i am using .xls & .xlxs files

it fails at OleDbDataReader dr = oledbcmd.ExecuteReader();

code:

> public partial class Upload : System.Web.UI.Page {
>     string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["PostbankConnectionString"].ConnectionString;
>     protected void Page_Load(object sender, EventArgs e)
>     {
> 
>     }
> 
>    public void importdatafromexcel(string excelfilepath)
>         {
>             //declare variables - edit these based on your particular situation
>             string ssqltable = "PPRS";
>             // make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have different
>             string myexceldataquery = "Select * FROM [PPR_Status_Detailed]";
>             try
>             {
>                 //create our connection strings
>                 string sexcelconnectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelfilepath +
> ";Extended Properties=" + "\"excel 12.0;hdr=yes;\"";
> 
>                 string sclearsql = "TRUNCATE TABLE " + ssqltable;
>                 SqlConnection sqlconn = new SqlConnection(strConnString);
>                 SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
>                 sqlconn.Open();
>                 sqlcmd.ExecuteNonQuery();
>               
>                 //series of commands to bulk copy data from the excel file into our sql table
>                 OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
>                 OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
>                 oledbconn.Open();
>                 OleDbDataReader dr = oledbcmd.ExecuteReader();
> 
>                 SqlBulkCopy bulkcopy = new SqlBulkCopy(strConnString);
>                 bulkcopy.DestinationTableName = ssqltable;
>                 //Mapping Table column    
> 
>                 bulkcopy.ColumnMappings.Add("Task ID","[Task_ID]");
>                 bulkcopy.ColumnMappings.Add("PPR Caption", "[PPR_Caption]");
>                 bulkcopy.ColumnMappings.Add("Project Start Date", "[Project_StartDate]");
>                 bulkcopy.ColumnMappings.Add("Project End Date", "[Project_EndDate]");
>                 bulkcopy.ColumnMappings.Add("Current Task", "[Current_Task]");
>                 bulkcopy.ColumnMappings.Add("User", "[User]");
>        
> 
> 
>                 sqlcmd.ExecuteNonQuery();
>                 while (dr.Read())
>                 {
>                     bulkcopy.WriteToServer(dr);
>                 }
> 
>                 oledbconn.Close();
>                 sqlconn.Close();
>              
>             }
> 
>             catch (Exception)
>             {
>                 //handle exception
>             }
>         }
>  
> 
>    }
> 
>    protected void Button1_Click(object sender, EventArgs e)    {
>        string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
>        importdatafromexcel(CurrentFilePath);     } }

Instead of :-

OleDbDataReader dr = oledbcmd.ExecuteReader();

Try this :-

DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt = oledbconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
OleDbDataAdapter sda = new OleDbDataAdapter(oledbcmd);
sda.Fill(ds);
dt = ds.Tables[0];

To read the excel name correctly please use below code:-

OleDbConn.Open();
DataTable dt = OleDbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string tableName = dt.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand OleDbCmd = new OleDbCommand($"SELECT * FROM [{tableName}]" , OleDbConn);

Please let me know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM