Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application where the user logs in and when they are validated the application needs to change to their database. I have done this in Windows APPS but I am new to the ASP.Net arena. Any help and or guidance is greatly appreciated.

I have attempted to search for a solution but I could find what I was looking for.

Thank you
Posted

1 solution

The ConnectionString where you specify the Database that should be used is a property of the Connection-Class. Just create a Connection-Object and then set the user-specific ConnectionString before using the Connection.

edit:

An excerpt from my database-access-class, maybe it helps:

C#
public class AdoDatabase
{
        public string ProviderName { get; private set; }
        public string ConnectionString { get; private set; }
        public DbProviderFactory ProviderFactory { get; private set; }

        public AdoDatabase(string providerName, string connectionString)
        {
            ProviderName = providerName;
            ConnectionString = connectionString;

            ProviderFactory = DbProviderFactories.GetFactory(providerName);
        }

        public DbConnection OpenConnection()
        {
            return OpenConnection(null);
        }

        public DbConnection OpenConnection(string databaseName)
        {
            DbConnection conn = ProviderFactory.CreateConnection();
            conn.ConnectionString = ConnectionString;
            conn.Open();
            if (databaseName != null)
                conn.ChangeDatabase(databaseName);
            return conn;
        }
}
 
Share this answer
 
v2
Comments
Dave-10169531 9-Mar-15 18:46pm    
Thank you. Would you happen to have an example?
Dave-10169531 9-Mar-15 18:48pm    
I have a Web.config file in project which has the connection strings I am not sure how to dynamically change then via the program or am I going about it the wrong way?
[no name] 9-Mar-15 18:54pm    
Where does the connection string from your web.config go? Or put differently, which technology do you use for database access? ADO.Net? Entity Framework?
Dave-10169531 9-Mar-15 18:58pm    
ADO.net
" <connectionstrings>
<add name="ApplicationServices" connectionstring="Data Source=m8ailfekg6.database.windows.net,1433;Initial Catalog=BAASoftware;Persist Security Info=True;User ID=baas;MultipleActiveResultSets=True;Connect Timeout=30" providername="System.Data.SqlClient">
<add name="MCConnStr" connectionstring="Data Source=ACERHOME\SQLEXPRESS;Initial Catalog=MC14051501;Integrated Security=True;Connect Timeout=30" providername="System.Data.SqlClient">
"
[no name] 9-Mar-15 19:00pm    
Don't you read the connection string from the web.config somewhere in your code?

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