1

i don't have any code for this theme. I have a "problem" with the access to many databases. I wrote a program where you can login and do some stuff (create reports etc). Bevore you login, the program should have a look in the local database to which system the user belongs. After that the connection string should change to get a connection to this system.

I need that because this reporting application is on our local server but the detailed datas of the users and every datas for creating reports are on the customer server. So step by step:

  1. User types in his login datas
  2. program should check the system for user in local db (like a table with "user1"-"system2")
  3. program should change constring or should use the constring for this system
  4. after that the login on this system starts in code behind
  5. user is logged in on his system

I've never done something like this and i read many times that it's not possible to change the constring in runtime. So i thought i can write every constring into the web.config and just tell him

If(user.system == 3){ use constring3 };

I really don't know how to handle this.

Thanks for every support.

Cheers Dave

Dave Stockinger
  • 139
  • 2
  • 18
  • 1
    What you are using? EF? If yes, `DbContext` contains proper comstructor where you can pass connecting string name. –  Sep 08 '14 at 08:11
  • oh yes sorry - c# and EF. DBContext? Is it that easy? I just thought the biggest problem is to change it while running?! – Dave Stockinger Sep 08 '14 at 08:13
  • 1
    you can change the connection string at runtime see if [this](http://stackoverflow.com/questions/5573973/how-to-retrieve-the-databasename-and-the-servername-from-the-connectionstring-in) and [this](http://stackoverflow.com/questions/6134359/read-connection-string-from-web-config) helps and also checkOut [this](http://msdn.microsoft.com/en-us/data/jj592674.aspx) link for EF – karthickj25 Sep 08 '14 at 08:14
  • ok the problem with changing it while running is that i have to restart the program after changing it - so i tried to set many constrings in the webconfig and i try to figure out how say that the EF or program should use `constring1` or `constring2` depending on the system in the db – Dave Stockinger Sep 08 '14 at 09:26
  • You shouldn't need to restart the program after changing it, but you will need to ensure that every time you use the DbContext you are using the correct connection string - this would typically be managed through the users session. – Zhaph - Ben Duguid Sep 08 '14 at 14:47

1 Answers1

0

A common aproach for situations like this would be using an IoC Container like Ninject. The Idea is to have only One Connection String (maybe wrapped inside a Class) which gets injected into your ViewModels.

Once the user logs in you will register the correct instance of this connection string to your container.

Read the Tutorial to figure out how it works.

MattC
  • 3,984
  • 1
  • 33
  • 49
Johannes Wanzek
  • 2,825
  • 2
  • 29
  • 47