.nettiers Changing connections at run time

1.2k views Asked by At

Are there any .netTiers ninjas out there that know how to change the database connection at run time?

I've found the following article on their documentation: http://www.nettiers.net/DataLayer.ashx

Towards the end, it has a code snippet demonstrating how to do this, but I've followed this snippet exactly, and it still doesn't seem to work.

here's my code (names have been changed to protect the innocent :-):

DataRepository.AddConnection("localDynamic", "Data Source=myserver;Initial Catalog=myDB;Integrated Security=True");

TList<Patient> patients = DataRepository.Connections["localDynamic"].Provider.PatientProvider.GetAll();

and here is the web.config - the connection string in the web config is commented out so that I can test changing the connection string dynamically:

<configuration>
  <configSections>
    <section name="dcPlatinum.Data"
    type="dcPlatinum.Data.Bases.NetTiersServiceSection, dcPlatinum.Data"
    allowDefinition="MachineToApplication"
    restartOnExternalChanges="true" />
  </configSections>
  <!--<connectionStrings>
    <add name="netTiersConnectionString" connectionString="Data Source=myServer;Initial Catalog=myDB;Integrated Security=True" />
  </connectionStrings>-->

  <dcPlatinum.Data defaultProvider="SqlNetTiersProvider">
    <providers>
      <add
        name="localDynamic"
        type="dcPlatinum.Data.SqlClient.SqlNetTiersProvider, dcPlatinum.Data.SqlClient"
        connectionStringName="localDynamic"
        providerInvariantName="System.Data.SqlClient"
        entityFactoryType="dcPlatinum.Entities.EntityFactory"
        useEntityFactory="true"
        enableEntityTracking="false"
        enableMethodAuthorization="false"
        useStoredProcedure="false"

        />
    </providers>
  </dcPlatinum.Data>
  <system.web>
      <compilation debug="true" targetFramework="4.0" />
  </system.web>

</configuration>

The problem that is happening is that it is not finding a connection string with the "localDyanmic" key...

thoughts?

1

There are 1 answers

0
Rob Bird On

You've probably figured this out by now, but just in case you're still trying....

We have dynamic connections working in our current project and the code you posted looks fine to me.

But I think you still need to reference a connection string that is registered in the web.config from the provider config section. So uncomment the connection string and then change the provider section to reference 'netTiersConnectionString' instead of 'localDynamic'.

netTiers should should ignore this and use localDynamic at runtime.

Also, be sure to run the DataRepository.AddConnection method once per connection string when the application starts. We run ours in the Application_Start method of Global.asax