Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've been looking through different sites on the internet for a solution to my problem. I need to manage multiple databases for a web app. Let's say that I have 2 clients, they will be having their own database (so that's 2 database), also i need to check if their account exist on my primary database. The code will basically target the primary database to check if their account exist, then programatically change the connection/ or point them to the database that they own. I'm tried to alter the web.config after checking their accounts, but this seems to be a bad program flow for each time a user attempts to log in, the web config will be altered and the logged in users will be directed to the new database connection.

I hope to hear a good answer from you guys.
Posted

What you are dealing with is Multi Tenancy. Not directly related to your question, but here is a good book on best practices in multi tenant environment.

http://msdn.microsoft.com/en-us/library/ff966499.aspx[^]

Coming back to you question, I don't think it's a good idea to maintain static connection strings for various tenants. Potentially, you can have 10 or even 100 tenants tomorrow.

My suggestion would be do implement a ConnectionStringManager sort of class which can give you the connection string for a specific client.
 
Share this answer
 
Comments
john villafania 21-Apr-14 5:55am    
thank you for correcting me with the term (Multi Tenancy). I'll take a look at the link you sent.

i'll be posting the code that i am using right now. please take a look at it.
thanks
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
            ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;
            var connection = WebConfigurationManager.ConnectionStrings["ClientConnection"].ConnectionString;
            string[] conn = connection.Split(';');
            string dataSource = conn[0];
            string initialCatalog = conn[1];
            string persistentSecInfo = conn[2];
            string userID = conn[3];

            string dbName=initialCatalog.Substring(16,9);

            if (!dbName.Equals(officeID))
            {
                connection = connection.Replace(dbName, officeID);
                section.ConnectionStrings["ClientConnection"].ConnectionString = connection;
                config.Save();
            }


This is the code I use to change my connection based on the officeID (which refers to the client's database name), I replace the value of the Initial Catalog to point to the database I need.

Any suggestions or comments?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900