在Excel数据驱动单元测试中使用数据源相对路径的错误。

时间:2022-12-13 04:35:05

I am using a System.Data.Odbc connection string to connect to an Excel data source. The following error occurs when using a relative dbq path:

我正在使用System.Data。连接到Excel数据源的Odbc连接字符串。使用相对dbq路径时发生以下错误:

ERROR [42S02] [Microsoft][ODBC Excel Driver] The Microsoft Office Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.

错误[42S02] [Microsoft][ODBC Excel驱动程序]Microsoft Office Access数据库引擎找不到对象'Sheet1$'。确保对象存在,并正确拼写其名称和路径名。

App.config:

App.config:

<configuration>
    <configSections>
        <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </configSections>
    <connectionStrings>
        <add name="excelConnection" connectionString="Dsn=Excel Files;Dbq=ExcelDataSource.xlsx;Defaultdir=.;driverid=790;maxbuffersize=2048;pagetimeout=5" providerName="System.Data.Odbc"/>
    </connectionStrings>
    <microsoft.visualstudio.testtools>
        <dataSources>
            <add name="excelDataSource" connectionString="excelConnection" dataTableName="Sheet1$" dataAccessMethod="Sequential"/>
        </dataSources>
    </microsoft.visualstudio.testtools>
</configuration>

Test Method Attributes:

测试方法属性:

[DeploymentItem("ExcelDataSource.xlsx")]
[DataSource("excelDataSource")]
[TestMethod]

Troubleshooting notes:

故障排除指出:

  • Works with an absolute dbq path
  • 使用绝对的dbq路径。
  • Excel file is set to 'Copy if Newer' and [DeploymentItem] is specified
  • Excel文件被设置为“复制更新”和“部署”
  • Fails before reaching the [TestInitialize] method; before the Test Deployment Dir is created.
  • 在到达[TestInitialize]方法之前失败;在创建测试部署目录之前。
  • It worked in the past, perhaps in an older Visual Studio version
  • 它在过去是有用的,可能是在一个较老的Visual Studio版本中

Question: Where does defaultDir=.; resolve to?

问题:哪里defaultDir =。决心?

1 个解决方案

#1


1  

It resolved correctly using |DataDirectory| variable: defaultDir=|DataDirectory|.

它正确使用|DataDirectory|变量:defaultDir=|DataDirectory|。

connectionString="Dsn=Excel Files;Dbq=ExcelDataSource.xlsx;Defaultdir=|datadirectory|\;driverid=790;maxbuffersize=2048;pagetimeout=5"

More on the |DataDirectory| variable here

更多关于|DataDirectory|变量的信息请参见这里

#1


1  

It resolved correctly using |DataDirectory| variable: defaultDir=|DataDirectory|.

它正确使用|DataDirectory|变量:defaultDir=|DataDirectory|。

connectionString="Dsn=Excel Files;Dbq=ExcelDataSource.xlsx;Defaultdir=|datadirectory|\;driverid=790;maxbuffersize=2048;pagetimeout=5"

More on the |DataDirectory| variable here

更多关于|DataDirectory|变量的信息请参见这里