views:

129

answers:

3

Hello all,

I'm looking at having certain users access one database and other users accessing another database based on the company they belong to. What would be the best way to handle the connection strings and make sure the user connects to the right db when they login?

Thanks for any ideas.

+3  A: 

Have a database table that stores the connection info for each user. Maybe something like the Provider Model would be useful?

StingyJack
Thanks, good info here.
Zero Cool
+6  A: 

I would use a dynamic connection string builder, and a master Database that mapped user accounts to other databases.

Instead of storing all those connection strings, I'd use my connection string builder method to create the connections as needed based on info from the master. I'd also use Windows Authentication, so as to avoid storing passwords in your database.

FlySwat
building on this you could add an extra column in the roles/accounts db if you are using that to contain this extra data.
MikeJ
+6  A: 

In Web.Config or App.Config

<connectionStrings>
  <add name="ConnectionForDudes" providerName="System.Data.SqlClient"
      connectionString="Data Source=___MALECONNECTIONHERE___"/>
  <add name="ConnectionForChicks" providerName="System.Data.SqlClient"
      connectionString="Data Source=___FEMALECONNECTIONHERE___"/>
</connectionStrings>

When it's time to open the database, you get your connection string this way:

bool UserIsMale = true;
string ConnectionStringName = "ConnectionFor" + UserIsMale ? "Dudes" : "Chicks";
string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString;
GeekyMonkey