0

I am experiencing a strange problem with a data driven unit test. The data is stored in an xls-file. When I use the following connection string, everything works fine.

    [TestMethod()]
    [DataSource(
        "System.Data.Odbc",
        @"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;dbq=MyFolder\TestData.xls;defaultdir=.",
        "Tabelle1$",
       DataAccessMethod.Sequential)]
    public void DataDrivenTest() {...}

What I would like to do now is to extract the filename from the connection string into some constant, e.g.

    const string FilePath = "MyFolder\TestData.xls";

But as soon as I start splitting up the connection string, I get errors in the unit test. Interestingly, even splitting the string into two parts does not work:

        ...
        @"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;" + @"dbq=MyFolder\TestData.xls;defaultdir=.",
        ...

Instead, I get the following error message

The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library.

Error details: ERROR [42S02] [Microsoft][ODBC Excel Driver] The Microsoft Jet database engine could not find the object 'Tabelle1$'. Make sure the object exists and that you spell its name and the path name correctly.

Any idea where this difference in behavior can come from?

What is most confusing for me, is that even in ildasm I could not see any obvious differences between the two variants (i.e, seemingly the compiler already concatenates the two strings).

4

1 回答 1

0

问题可能在于使用@添加字符串,您可以尝试:

"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;" + "dbq=MyFolder\\TestData.xls;defaultdir=."
于 2012-05-22T11:32:05.087 回答