Is it possible to get the connection string from the app.config file for the DataSourceAttribute

Hello,

I would like to be able to change my DataSource Attribute connection string to access the app.config file. The problem that I am running into is that the connecting string must be a constant. Do you know of any work suggestions?

Currently it is something like this.

[DataSource("System.Data.SqlClient", "Server=TestServer;DataBase=TestDB;User Id=sa;Password=", "ChildSearchCriteria",DataAccessMethod.Sequential)]

I would like it to be something like this.

[DataSource("System.Data.SqlClient",ConfigurationManager.AppSettings["MyTestDB"], "TestTable",DataAccessMethod.Sequential)]

ThanksSmile

[2153 byte] By [GregoryDye] at [2008-3-7]
# 1

Yes--there is in fact a way to do this. If you add some particular sections to the app.config file, you can use a different constructor for the DataSource attribute to automatically retrieve the connection string from it.

First, define the microsoft.visualstudio.qualitytools section under <configuration>/<configSections>:

<section name="microsoft.visualstudio.qualitytools" type="Microsoft.VisualStudio.QualityTools.UnitTesting.Framework.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=8.0.0.0, PublicKeyToken=b03f5f7f11d50a3a" />

Note that <configSections> must be the first section is the .config file.

Then add the connection string and provider name in the <configuration>/<connectionStrings> section, e.g.:

<connectionStrings>
<add name="MyConnectionString" connectionString='Provider="Microsoft.Jet.OLEDB.4.0";Data Source="Data.xls";Persist Security Info=False;Extended Properties="Excel 8.0"' providerName="System.Data.OleDb" />
</connectionStrings>

Finally you will need to specify the rest of the connection information (data table, data access method). Those are specified in the <configuration>/<microsoft.visualstudio.qualitytools>/<dataSources> section, as well as the name of the previously specified connection string to be used, e.g.:

<microsoft.visualstudio.qualitytools>
<dataSources>
<add name="MyDataSource" connectionString="MyConnectionString" dataTableName="DataTable$" dataAccessMethod="Sequential"/>
</dataSources>
</microsoft.visualstudio.qualitytools>

Then in the code you simply use [DataSource("MyDataSource")] to refer to that entry in the app.config file. This should do what you're looking for. Keep in mind that for Beta 2, you need to set the app.config file to copy to the output directory, since it isn't copied automatically when you build a test project.

KevinCoggerMSFT at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 2

Thanks, but now I am getting this error which is related to the datasources.

Any help would be great!Big Smile Thanks again.

Unit Test Adapter threw exception: System.Configuration.ConfigurationErrorsException: Data source 'MyDataSource' cannot be found in the test configuration settings.
at Microsoft.VisualStudio.QualityTools.Tips.UnitTest.UnitTestRunner.InitTestFromConfigFile(UnitTestElement test)
at Microsoft.VisualStudio.QualityTools.Tips.UnitTest.UnitTestRunner.ExecuteDataDrivenTest(UnitTestElement test, ITestContext testContext, UnitTestAdapterContext userContext, Boolean isLoadTest)
at Microsoft.VisualStudio.QualityTools.Tips.UnitTest.UnitTestRunner.Run(UnitTestElement test, ITestContext testContext, Boolean isLoadTest).

Here is my configuration file.

<?xml version="1.0" encoding="utf-8" ?>
<
configuration>
<
configSections>
<
section name="Microsoft.VisualStudio.QualityTools" type="Microsoft.VisualStudio.QualityTools.UnitTesting.Framework.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=8.0.0.0, PublicKeyToken=b03f5f7f11d50a3a"/>
</
configSections>

<Microsoft.VisualStudio.QualityTools>
<
dataSources>
<
add name="MyDataSource" connectionString="MyConnectionString" dataTableName="ChildSearchCriteria" dataAccessMethod="Sequential" />
</
dataSources>
</
Microsoft.VisualStudio.QualityTools>

<connectionStrings>
<
add name="MyConnectionString" connectionString="Server=localhost;DataBase=MyTest;User Id=sa;Password=" providerName="System.Data.SqlClient"/>
</
connectionStrings>

</configuration>

GregoryDye at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 3
Okay, I seem to have this figured out. I'm not sure exactly why this is, but I was able to get it working after changing two things. First, in <configSections>, you need to specify Culture=neutral after Version=8.0.0.0 when indicating the type. Second, every instance of Microsoft.VisualStudio.QualityTools should be all lowercase. So the <configSections> section looks like this:

<configSections>
<
section name="microsoft.visualstudio.qualitytools" type="Microsoft.VisualStudio.QualityTools.UnitTesting.Framework.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
/>
</
configSections
>

And the data sources section looks like this:

<microsoft.visualstudio.qualitytools>
<
dataSources
>
<
add name="MyDataSource" connectionString="MyConnectionString" dataTableName="ChildSearchCriteria" dataAccessMethod="Sequential"
/>
</
dataSources
>
</
microsoft.visualstudio.qualitytools
>

It should work with those changes. I'll see if we can do something about the all-lowercase requirement by the final release, since that seems to make this unnecessarily difficult to get right.

KevinCoggerMSFT at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 4
Thanks, That fixed my problem.
GregoryDye at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...
# 5

Has something changed since this post? I'm using RC1 and the code posted here doesn't even compile.

[DataSource("MyDataSource")]

Using the above code, I get the following error:

Error 1 No overload for method 'DataSourceAttribute' takes '1' arguments M:\WebTests\MyTests\Test0\TestDynamic.cs 27 6 WebTests

Mountain at 2007-9-8 > top of Msdn Tech,Visual Studio Team System,Visual Studio Team System - Web and Load Testing...

Visual Studio Team System

Site Classified