Solved

Asp.net on the fly connection string changes

Posted on 2007-04-03
9
714 Views
Last Modified: 2013-11-26
I am building a VS2005 ASP.net C# application that requires database access
with on the fly connection string changes based on user logged in.

All that needs to change is the database name / server name
using a trusted connection and no user password is stored in the string.

This is what I was thinking of doing.. or do you have a better
solution?

Create a class that uses the connection string builder class
look up the user name and the associated db server/dbname stored
in a database  table and return the connection string.
On each page that requires access to the db use the class to set the connection string .

Thanks
0
Comment
Question by:mustang_larry
  • 3
  • 2
9 Comments
 
LVL 1

Expert Comment

by:cmwhite
ID: 18845954
I personally in the past have done roughly that, except had my Connection String roughly prebuilt so no reason for the connection builder. IE:

string u = Request("name");

string cs = "Driver={SQL Server};Server=myServerAddress;Database=" + u + "Trusted_Connection=Yes";

SqlConnection sqlconn;

etc..

That would work for your case, but mine was a bit different I did require a specifc password to verify against so I did the same thing though except passed it encrypted.

Hope this helps

Chris
0
 
LVL 21

Expert Comment

by:Yurich
ID: 18846385
a little flaw in the logic... you need a connection string to connect to a database to look up in the table for a connection string... unless of course you're going to have some kind of system connection string and then customizable customer/user connection string...

Depending on the number of databases it would probably make sense to keep it in web.config file or if it's too big, probably in some separate file (possible xml file as it can be structured in proper data-storage way). having a separate "system" connection is acceptable also, but little bit strange.

Having a separate class for this seems to me a bit overdoing. you can initialize your connection string once when you user logs in, and then you either can pass your connection string along the pages (using session variable) or initialize it on each page.

good luck,
yurich
0
 

Author Comment

by:mustang_larry
ID: 18846553
Yurich thanks for your ideas

My thought was in the web.config have one connection string to the "master" users
database which the class would use.

The main goal is to have one set of aspx pages for all users.

The number of connection strings would be 20-50 for now but could grow.

I am using the Membership Classes was thinking that adding the server and dbnames
linked to  the users id. I want to keep things clean instead of maintaining separate xml file or
web.conf entries since they would really need to be synced when users
are added or removed.

The session variable sounds like a good idea this way it won't have to
make multiple db requests for each page load.. As long as this isn't
a security risk.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 21

Accepted Solution

by:
Yurich earned 125 total points
ID: 18847208
The session variable is quite secure as it's server-based (it does consume some server resources, but it's reasonable in your situation).

I didn't realize it would be 20-50 variations, I thought of 3-4, in whcih case it does make sense to keep your master connection settings in web.config and everything else in a master database.

Cheers,
Yurich
0
 

Author Comment

by:mustang_larry
ID: 18853890
Anybody else done this? and pros cons other possible methods?
0
 
LVL 21

Expert Comment

by:Yurich
ID: 19250700
I'm still interested.

Regards,
Yurich
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question