locked
Dynamic DataSourceAttribute Connection string RRS feed

  • Question

  • Hi

    I want my DataSource property with the connection string and table name like

    [DataSource(connectionString, tableName)], here the connectionstring value should be configurable(in app.config or any config file)

    because the databse server details will keep on change.I have 5000 plus tables, so i cannot keep the datasource section in config file and read that in DataSource attribue.


    Is that a possible scenario, like the connection string values are configurable in app.config file and the table name is specified in the Datasource property?

    [DataSource(connectionString, tableName)]

    <connectionStrings>

    <add name="UnitTestDataConnectionString"             connectionString="Data Source="(loacldb); Inital Catalog:MyTestDB; Integrated Security=True"             providerName="System.Data.SqlClient" />    </connectionStrings>

    [DataSource("UnitTestDataConnectionString","testTable")]

    how can we configure the datasource like this?

    Wednesday, March 29, 2017 10:15 AM

All replies

  • you can follow this: Walkthrough: Using a Configuration File to Define a Data Source

    you should define Data Sources in config file, not just connection string. And then you can use DataSourceAttribute with data source setting name which you already defined in config file.

    Thursday, March 30, 2017 5:51 AM
  • Hi ArunkumarN

    Thanks for your post!

    >> I have 5000 plus tables, so i cannot keep the datasource section in config file

    I'm not so sure about above statement, does it mean you got any errors when you specified the config file?

    Usually, we use a configuration file to define a data source same as wheeler xie's reply

    If yes, please refer to the following steps:

    1. Create the app.config file
    2. Custom content in config file
    3. Create a Unit Test Using Data Sources Defined

    The app.config file should look similar to this(Connect Access):

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <!--The assembly name must match the Microsoft Visual Studio .NET Framework build that you are using-->
        <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      </configSections>
      <connectionStrings>    
        <!--connect to Microsoft Access-->
        <add name="MyJetConn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\testdatasource.accdb; Persist Security Info=False;" providerName="System.Data.OleDb" />
      </connectionStrings>
      <microsoft.visualstudio.testtools>
        <dataSources>
          <add name="MyJetDataSource" connectionString="MyJetConn" dataTableName="MyDataTable" dataAccessMethod="Sequential"/>
        </dataSources>
      </microsoft.visualstudio.testtools>
    </configuration>
    

    This is example code using app.config:

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Data;
    
    namespace ClassLibrary1Tests
    {
    [TestClass]
        public class UnitTest1
        {
            private TestContext context;
    
           public TestContext TestContext
            {
                get { return context; }
                set { context = value; }
            }
    
            [TestMethod()]
            [DeploymentItem("MyTestProject\\testdatasource.accdb")]
            [DataSource("MyJetDataSource")]
            public void MyTestMethod()
            {
                int a = Int32.Parse(context.DataRow["Arg1"].ToString());
                int b = Int32.Parse(context.DataRow["Arg2"].ToString());
                Assert.AreNotEqual(a, b, "A value was equal.");
            }
        }
    }
    

    In addition, this is related link about how to using a configuration file to define a data source:

    https://msdn.microsoft.com/en-us/library/ms243192.aspx?f=255&MSPPError=-2147217396

    Please feel free to let me know if I misunderstood.

    Best regards,

    Fletch


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 30, 2017 7:53 AM
  • Thanks Wheeler xie, is there any other way that i can use without Datasource section in config file?

    Can you guide me whats is the use of this method

    DataSource(connectionString, tableName), can you give any example for this method, i am using SQL DB

    Friday, March 31, 2017 2:23 PM
  • Thanks Fletch,

    There is no error while specifying the datasources in config file. But i want to use the method

    DataSource(connectionString, tableName), can you give me a sample for this method. I am using SQL DB


    Friday, March 31, 2017 2:26 PM
  • Hi friend,

    >> But I wan to use the method DataSource(connectionString, tableName)

    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")]

    As you mentioned dynamic, I think the second method works for you.

    Please feel free to let me know if I misunderstood.

    Best regards,

    Fletch


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Sunday, April 2, 2017 1:05 PM