DataSourceAttribute Class
Provides data source-specific information for data-driven testing. This class cannot be inherited.
Assembly: Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)
| Name | Description | |
|---|---|---|
![]() | DataSourceAttribute(String^) | Initializes a new instance of the DataSourceAttribute class. This instance will be initialized with a data provider and connection string associated with the setting name. |
![]() | DataSourceAttribute(String^, String^) | Initializes a new instance of the DataSourceAttribute class. This instance will be initialized with a connection string and table name. |
![]() | DataSourceAttribute(String^, String^, String^, DataAccessMethod) | Initializes a new instance of the DataSourceAttribute class. This instance will be initialized with a data provider, connection string, data table and data access method to access the data source. |
| Name | Description | |
|---|---|---|
![]() | ConnectionString | Gets a value representing the connection string for the data source. |
![]() | DataAccessMethod | Gets the method used to access the data source. |
![]() | DataSourceSettingName | Gets a value indicating the setting name used to identify data source connection information stored in a configuration file. |
![]() | ProviderInvariantName | Gets a value representing the data provider of the data source. |
![]() | TableName | Gets a value indicating the table name providing data. |
![]() | TypeId | (Inherited from Attribute.) |
| Name | Description | |
|---|---|---|
![]() | Equals(Object^) | (Inherited from Attribute.) |
![]() | GetHashCode() | (Inherited from Attribute.) |
![]() | GetType() | (Inherited from Object.) |
![]() | IsDefaultAttribute() | (Inherited from Attribute.) |
![]() | Match(Object^) | (Inherited from Attribute.) |
![]() | ToString() | (Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | DefaultDataAccessMethod | Represents the default data access method. This field is read-only. |
![]() ![]() | DefaultProviderName | Represents the default data provider name. This field is read-only. |
| Name | Description | |
|---|---|---|
![]() ![]() | _Attribute::GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | (Inherited from Attribute.) |
![]() ![]() | _Attribute::GetTypeInfo(UInt32, UInt32, IntPtr) | (Inherited from Attribute.) |
![]() ![]() | _Attribute::GetTypeInfoCount(UInt32) | (Inherited from Attribute.) |
![]() ![]() | _Attribute::Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | (Inherited from Attribute.) |
The DataSourceAttribute class provides two ways to specify data source information for data-driven tests. The first way specifies information through a connection string, provider information, and source table name passed to the DataSourceAttribute.
Connection String Example:
[DataSource("Provider=SQLOLEDB.1;Data Source=MySource;Integrated] Security=SSPI;Initial Catalog=MyCatalog;Persist Security Info=False", "MyTable")]
The second way passes a single argument to the attribute that specifies the configuration setting located in the app.config file.
Configuration Setting Example:
[DataSource("dataSourceNameFromConfigFile")]
Note |
|---|
Different data providers use different connection strings. The name of the provider is a part of connection string. |
For more information about using the app.config file for specifying a data source, see Walkthrough: Using a Configuration File to Define a Data Source.
For more information about data-driven tests, see Overview of Data-Driven Unit Tests (NIB).
For more information about using attributes, see Extending Metadata Using Attributes.
Legacy Code Example
The following code contains the class and method to test.
The following test will pass. It uses the sample.mdb access database that contains the following data in Table1.
Name | Balance | Amount |
|---|---|---|
Jorg Bott | 100 | 25 |
Pedro Ruivo | 70 | 60 |
Mandar Samant | 75 | 71.25 |
Russell King | 159 | 158 |
Jun Cao | 11.99 | 11.22 |
Note that the DataAccessMethod is sequential.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.






