multiple connection string in web.config

Discussion in 'ASP .Net' started by wild, Oct 24, 2007.

  1. wild

    wild Guest

    My development machine has Win XP pro and IIS 5.1
    I have a 'default web site' with 2 virtual directories, let's say
    'siteA' and 'siteB'.
    Both point to the same application in the same phisical directory,
    let's say C:\web\mysite.
    In the 'web.config' file I have a key ('mysqlstring') that defines the
    connection string to the database.
    Is it possible to have two connection strings depending one the site I
    use?
    So if I came from 'http://localhost/siteA' I will use database 'A '
    and if I came from 'http://localhost/siteB' I will use database 'B'
    with the same application?
    Thank you for your replay
     
    wild, Oct 24, 2007
    #1
    1. Advertising

  2. You have as many connection strings as you wish:

    <connectionStrings>

    <add name="ConnectionString1" connectionString="..."/>

    <add name="ConnectionString2" connectionString="..."/>

    </connectionStrings>

    and in the code you can check the request url to decide what connection
    string to use. Page.Request property points to the HttpRequest object that
    in turn provides various url-related properties.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "wild" <> wrote in message
    news:...
    > My development machine has Win XP pro and IIS 5.1
    > I have a 'default web site' with 2 virtual directories, let's say
    > 'siteA' and 'siteB'.
    > Both point to the same application in the same phisical directory,
    > let's say C:\web\mysite.
    > In the 'web.config' file I have a key ('mysqlstring') that defines the
    > connection string to the database.
    > Is it possible to have two connection strings depending one the site I
    > use?
    > So if I came from 'http://localhost/siteA' I will use database 'A '
    > and if I came from 'http://localhost/siteB' I will use database 'B'
    > with the same application?
    > Thank you for your replay
    >
     
    Eliyahu Goldin, Oct 24, 2007
    #2
    1. Advertising

  3. wild

    wild Guest

    On 24 Ott, 14:30, "Eliyahu Goldin"
    <> wrote:
    > You have as many connection strings as you wish:
    >
    > <connectionStrings>
    >
    > <add name="ConnectionString1" connectionString="..."/>
    >
    > <add name="ConnectionString2" connectionString="..."/>
    >
    > </connectionStrings>
    >
    > and in the code you can check the request url to decide what connection
    > string to use. Page.Request property points to the HttpRequest object that
    > in turn provides various url-related properties.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net


    Thank you for your replay but I don't want to correct the code already
    written in a lot of routines,
    something like "Dim MyConn As New
    OdbcConnection(ConfigurationSettings.AppSettings("mySQLString"))".
    I was asking if it is possible to have two different 'config' files
    depending on the requesting url, and leave the code unchanged.
     
    wild, Oct 24, 2007
    #3
  4. wild

    wild Guest

    On 24 Ott, 14:30, "Eliyahu Goldin"
    <> wrote:
    > You have as many connection strings as you wish:
    >
    > <connectionStrings>
    >
    > <add name="ConnectionString1" connectionString="..."/>
    >
    > <add name="ConnectionString2" connectionString="..."/>
    >
    > </connectionStrings>
    >
    > and in the code you can check the request url to decide what connection
    > string to use. Page.Request property points to the HttpRequest object that
    > in turn provides various url-related properties.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net


    Thank you for your replay but I don't want to correct the code already
    written in a lot of routines,
    something like "Dim MyConn As New
    OdbcConnection(ConfigurationSettings.AppSettings("mySQLString"))".
    I was asking if it is possible to have two different 'config' files
    depending on the requesting url, and leave the code unchanged.
     
    wild, Oct 24, 2007
    #4
  5. Did you have a look at asp.net configuration settings under virtual directory
    properties in IIS?

    Also, you could have two start pages, one for each virtual directory, which
    sets the database connection.
    Thanks
    ---------------------------
    Thanks,
    Ibrahim

    Software Consultant - Web Development, GB


    "wild" wrote:

    > On 24 Ott, 14:30, "Eliyahu Goldin"
    > <> wrote:
    > > You have as many connection strings as you wish:
    > >
    > > <connectionStrings>
    > >
    > > <add name="ConnectionString1" connectionString="..."/>
    > >
    > > <add name="ConnectionString2" connectionString="..."/>
    > >
    > > </connectionStrings>
    > >
    > > and in the code you can check the request url to decide what connection
    > > string to use. Page.Request property points to the HttpRequest object that
    > > in turn provides various url-related properties.
    > >
    > > --
    > > Eliyahu Goldin,
    > > Software Developer
    > > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >
    > Thank you for your replay but I don't want to correct the code already
    > written in a lot of routines,
    > something like "Dim MyConn As New
    > OdbcConnection(ConfigurationSettings.AppSettings("mySQLString"))".
    > I was asking if it is possible to have two different 'config' files
    > depending on the requesting url, and leave the code unchanged.
    >
    >
    >
    >
     
    =?Utf-8?B?SWJyYWhpbSBTaGFtZWVxdWU=?=, Oct 24, 2007
    #5
  6. wild

    Reggie Guest

    You could set the connection string as follows (air code). Then all you
    have to do is run the replace command on the application to remove the
    parenthesis around ("mySQLString") so that its (mySQLString). So if you had
    the following 2 connection strings:

    <add name="ConnectionString1" connectionString="..."/>
    <add name="ConnectionString2" connectionString="..."/>

    Page_Load
    Dim mySQLString As String
    Dim strSite As String

    strSite = Request.QueryString("Site")

    Select Case strSite
    Case SiteA
    mySQLString = "ConnectionString1"
    Case SiteB
    mySQLString = "ConnectionString2"
    End Select

    conn_str = ConfigurationSettings.AppSettings(mySQLString )
    conn = New SqlConnection(conn_str)
    conn.Open()

    Hope this helps!
    --

    ******************
    Reggie

    "Ibrahim Shameeque" <> wrote in
    message news:...
    > Did you have a look at asp.net configuration settings under virtual
    > directory
    > properties in IIS?
    >
    > Also, you could have two start pages, one for each virtual directory,
    > which
    > sets the database connection.
    > Thanks
    > ---------------------------
    > Thanks,
    > Ibrahim
    >
    > Software Consultant - Web Development, GB
    >
    >
    > "wild" wrote:
    >
    >> On 24 Ott, 14:30, "Eliyahu Goldin"
    >> <> wrote:
    >> > You have as many connection strings as you wish:
    >> >
    >> > <connectionStrings>
    >> >
    >> > <add name="ConnectionString1" connectionString="..."/>
    >> >
    >> > <add name="ConnectionString2" connectionString="..."/>
    >> >
    >> > </connectionStrings>
    >> >
    >> > and in the code you can check the request url to decide what connection
    >> > string to use. Page.Request property points to the HttpRequest object
    >> > that
    >> > in turn provides various url-related properties.
    >> >
    >> > --
    >> > Eliyahu Goldin,
    >> > Software Developer
    >> > Microsoft MVP
    >> > [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >>
    >> Thank you for your replay but I don't want to correct the code already
    >> written in a lot of routines,
    >> something like "Dim MyConn As New
    >> OdbcConnection(ConfigurationSettings.AppSettings("mySQLString"))".
    >> I was asking if it is possible to have two different 'config' files
    >> depending on the requesting url, and leave the code unchanged.
    >>
    >>
    >>
    >>
     
    Reggie, Oct 25, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michelle A.
    Replies:
    0
    Views:
    567
    Michelle A.
    Aug 20, 2003
  2. Replies:
    0
    Views:
    1,598
  3. CSharpner
    Replies:
    0
    Views:
    1,109
    CSharpner
    Apr 9, 2007
  4. Replies:
    3
    Views:
    858
    Samuel R. Neff
    Aug 3, 2007
  5. nospam
    Replies:
    0
    Views:
    712
    nospam
    Oct 5, 2007
Loading...

Share This Page