locked
How to change connection strings at runtime RRS feed

  • Question

  • User2117188934 posted

    Opportunity:

    I am developing an ASP.NET 4 SaaS application (Migrating a windows application) that uses a sql database, but the data is unique to each client.

    I have one database that contains the client id and stores the database connection information and the actual client database.

    In a Win Forms app (VB) I figured out how to have one login window with client id, user id and password. When client would click on the log in button it would validate their client ID and pull the DB connection information and open the client DB validate the User ID and Password and make the database available to the rest of the application.

    I am new to the ASP.NET world and am having trouble figuring out how to do this and make the transition.

    Assistance would be greatly appreciated.

    Can anyone point me in the right direction?

    Wednesday, February 18, 2015 3:59 PM

Answers

  • User-821857111 posted

    If you want to retain data on a user-by-user basis. one way to do this is using Sessions. When the user logs in, you obtain their connection string from your db and store it as a session variable:

    Session["ConnString"] = their_connection_string;

    Then you can pass that value to whatever needs it:

    using(SqlConnection conn = new SqlConnection(Session["ConnString"]))

    You must check that Session["ConnString"] is not null before attempting to use it. as session data is stored in Server RAM and is volatile. If it is null, just repopulate the variable with data from the database.

    Ref: ASP.NET Session State Overview

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 19, 2015 2:31 AM
  • User1918509225 posted

    Hi David55,

    As far as i know ,when you user log in the system,you first check its  user id and password.

    After  user has been validated successfully ,you could get the connections based on the user id .

    Then create connection to database like below:

    SqlConnection conn=new SqlConnection("yourconnectionstring")

    Best Regards,

    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 19, 2015 3:18 AM

All replies

  • User-821857111 posted

    If you want to retain data on a user-by-user basis. one way to do this is using Sessions. When the user logs in, you obtain their connection string from your db and store it as a session variable:

    Session["ConnString"] = their_connection_string;

    Then you can pass that value to whatever needs it:

    using(SqlConnection conn = new SqlConnection(Session["ConnString"]))

    You must check that Session["ConnString"] is not null before attempting to use it. as session data is stored in Server RAM and is volatile. If it is null, just repopulate the variable with data from the database.

    Ref: ASP.NET Session State Overview

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 19, 2015 2:31 AM
  • User1918509225 posted

    Hi David55,

    As far as i know ,when you user log in the system,you first check its  user id and password.

    After  user has been validated successfully ,you could get the connections based on the user id .

    Then create connection to database like below:

    SqlConnection conn=new SqlConnection("yourconnectionstring")

    Best Regards,

    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 19, 2015 3:18 AM