简体   繁体   English

当我尝试在SQL中导入Excel电子表格时出现错误

[英]When I try to import an Excel spreadsheet in SQL I get an error

Below is my code, any help will be greatly appreciated 下面是我的代码,任何帮助将不胜感激

:The Microsoft Office Access database engine could not find the object 'Breach'. :Microsoft Office Access数据库引擎找不到对象“突破”。 Make sure the object exists and that you spell its name and the path name correctly. 确保对象存在,并且正确拼写了它的名称和路径名。

public void importdatafromexcel(string excelfilepath)
{
    string ssqltable = "Testing";

    string myexceldataquery = "select [Case Owner],[Case Number],[Severity] from [Breach]";
    try
    {
        string sexcelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelfilepath + ";Extended Properties=Excel 12.0;";
        string ssqlconnectionstring = "Persist Security Info=False;Integrated Security=true;Initial Catalog=0Breach;server= CHAUDAHARI-J1-W\\SQLEXPRESS";

        string sclearsql = "delete from " + ssqltable;
        SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
        SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
        sqlconn.Open();
        sqlcmd.ExecuteNonQuery();
        sqlconn.Close();

        OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
        OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
        oledbconn.Open();
        OleDbDataReader dr = oledbcmd.ExecuteReader();
        SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring);
        bulkcopy.DestinationTableName = ssqltable;
        while (dr.Read())
        {
            bulkcopy.WriteToServer(dr);
        }

        oledbconn.Close();
    }
    catch (Exception ex)
    {
        txtProcessingStatus.Text = ex.Message;
    }
}
select [Case Owner],[Case Number],[Severity] from [Breach]

Is there a worksheet named "Breach" in the Excel file you are opening? 您要打开的Excel文件中是否有一个名为“突破”的工作表? In your statement above, I believe [Breach] should be the name of the worksheet you are looking for in the Excel file you are opening. 在上面的声明中,我相信[突破]应该是您要在打开的Excel文件中查找的工作表的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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