[英]The Microsoft Office Access database engine could not find the object 'PPR_Status_Detailed'
I am trying to upload excel spreadsheet to sql database through c# asp.net web page. 我正在尝试通过c#asp.net网页将excel电子表格上传到sql数据库。 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." 我不断收到错误消息:“ Microsoft Office Access数据库引擎找不到对象'PPR_Status_Detailed',请确保该对象存在并且您正确拼写了它的名称和路径名。” 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" . 当我打开excel文件,然后尝试上传该文件时,它显示“外部表未采用预期格式”。 i am using .xls & .xlxs files 我正在使用.xls和.xlxs文件
it fails at OleDbDataReader dr = oledbcmd.ExecuteReader(); 它在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:- 要正确读取excel名称,请使用以下代码:-
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. 请让我知道这可不可以帮你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.