Datalayer to use the right connection string base on URL of page request

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pod
    Contributor
    • Sep 2007
    • 298

    Datalayer to use the right connection string base on URL of page request

    Data layer to use the right connection string based on URL of page request

    On my computer I am developing a 3 tier Web application which I publish as two different versions, one for production and one for development purposes: IMP-PROD for production and IMP-DEV for developement. Both connect to their own database: respectively IMPDB-PROD and IMPDB-DEV.

    I have one Server with IIS and MS SQL for both Web applications. Each web app have the same hosting url but with a different sub folder name (similar to virtual folders).

    I would like the Data Layer to auto detect its URL as to connect to the right database.

    The web.config files contains both connection strings,
    and I have a Public Class in the data layer that reads those strings

    From the datalayer I can detect the CultureInfo but I cannot get session variables which I could use to detects the URL.

    Can someone offer me suggestions?


    Thanks

    P:oD


    Code:
    ----- presentation layer -------
    ------- web.config ------
    <add name="SqlConnIMP-PROD" 
             connectionString="data source=(local)\sqlexpress;Database=IMP;Trusted_Connection=True"
             providerName="System.Data.SqlClient"/>
    <add name="SqlConnIMP-DEV"
             connectionString="data source=(local)\sqlexpress;Database=IMP-DEV;Trusted_Connection=True"
             providerName="System.Data.SqlClient"/>
    
    -------- data layer ----------
    Public Class DatabaseHelper
    
        Friend Shared Function GetSqlConnIMP() As SqlConnection
            Dim RightConnectionstring As String = "SqlConnIMP-PROD"
            'Good Code needed here
            If DevEnvironment Then
                RightConnectionstring = "SqlConnIMP-DEV"
            End If
    
            Return New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings(RightConnectionstring).ConnectionString)
        End Function
    Last edited by pod; Sep 19 '14, 02:24 PM. Reason: typo
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Use #IF DEBUG to determine if your build is for debugging purposes. If it is, use your development connection string, otherwise use the production connection string.

    -Frinny

    Comment

    • pod
      Contributor
      • Sep 2007
      • 298

      #3
      :) That is exactly what I started to use after posting my question, and it's working very well except for one section: the SqlProfileProvi der in web.config requires to know which connections string to use.
      But, I just found out that the web.config can have some sub scripts: web.release.con fig and web.debug.confi g... I am reading up on it right now
      Last edited by pod; Sep 22 '14, 05:20 PM. Reason: typo

      Comment

      • pod
        Contributor
        • Sep 2007
        • 298

        #4
        Found one post that explains it in simple terms Scott's Blog (some posts are so confusing)

        Although the Match and Replace do not work exactly as they say, they at least works at the parent level:
        I wanted to replace only one connection string but it replaces all strings but without touching the other configurations. .. good enough for me

        Thank you all, I hope this helps someone else out there


        P:oD

        Comment

        Working...