multiple connection string in web.config

W

wild

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
 
E

Eliyahu Goldin

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
 
W

wild

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.

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.
 
W

wild

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.

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.
 
G

Guest

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
 
R

Reggie

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 said:
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 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top