【问题标题】:Format of the initialization string does not conform to the OLE DB specification初始化字符串的格式不符合 OLE DB 规范
【发布时间】:2015-10-18 01:42:51
【问题描述】:

尝试使用第三方组件读取 Excel 文件时出现以下错误

初始化字符串的格式不符合 OLE DB 规范

现在,我知道“第三方组件”这个词会在这里敲响警钟,但请听我说完。

这是我正在使用的连接字符串

Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\users\rory\downloads\testdb_4.xls;
Extended Properties='Excel 8.0;HDR=YES;';

我有这个 exact 连接字符串可以在以下 C# 代码中正常工作

using (OleDbConnection conn = new OleDbConnection())
{
    DataTable dt = new DataTable();
    conn.ConnectionString = connstring;
    using (OleDbCommand comm = new OleDbCommand())
    {
        comm.CommandText = "select * from [TEST_DB$]"; 
        // TEST_DB is the name of the sheet
        comm.Connection = conn;
        using (OleDbDataAdapter da = new OleDbDataAdapter())
        {
            da.SelectCommand = comm;
            da.Fill(dt);
            // do stuff with dt
        }
    }
}

这按预期工作,数据表中填充了 Excel 文件中的数据。但是,当我尝试从组件访问它时,出现上述错误。

我不是 OLE DB 方面的专家,但我的印象是驱动程序处于操作系统级别,如果它们适用于一个应用程序/连接字符串,它们应该适用于具有相同连接字符串的所有应用程序。我错了吗?如果是这样,有没有人知道这里发生了什么?

我已联系该组件的支持人员。

【问题讨论】:

    标签: c# excel oledb


    【解决方案1】:

    解决方案最终是用转义的双引号替换单引号。

     string conn = "Provider=Microsoft.ACE.OLEDB.12.0;" + 
     "Data Source=C:\users\rory\downloads\testdb_4.xls;" + 
     "Extended Properties='Excel 8.0;HDR=YES;'"; // single quotes
    

    成为

     string conn = "Provider=Microsoft.ACE.OLEDB.12.0;" + 
     "Data Source=C:\users\rory\downloads\testdb_4.xls;" + 
     "Extended Properties=\"Excel 8.0;HDR=YES;\""; // escaped double quotes
    

    OleDbConnection 对单引号没有问题,但显然某些应用程序和组件会这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-04
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多