开发者

Connection strings for different users

开发者 https://www.devze.com 2023-03-01 01:58 出处:网络
How do I send two users coming from different company domains to different SQL databases to retrieve/store data? I use Application variables to store the connection strings and the Request.ServerVaria

How do I send two users coming from different company domains to different SQL databases to retrieve/store data? I use Application variables to store the connection strings and the Request.ServerVariables("LOGON_USER") variable is an effective way to get the domain name. Is the GLOBAL.AsA file to be modified? The ta开发者_高级运维ble names are exactly the same in both databases, so I think changing the connection strings based on the user domain should do the trick.

User A with domain ABC --> Application("ConnecttoDB") send to database A User B with domain XYZ --> Application("ConnecttoDB") send to database B

I have roughly 900+ classic ASP pages so I would really hate to add a bunch of IF-THEN's to choose the correct database in each page. All ideas are greatly appreciated!

UPDATE: To make things simple I'm envisioning one single Application variable (i.e.: ConnecttoDB) However, wouldn't its value be constantly changing every time a different user gets access and altering page results?


You can't use an Application variable since that's shared across all users. This would be a race condition. Instead you'll need to use the Session object to store the connection and then use that whenever you need to connect to the DB.

myDB=Server.CreateObject("ADODB.Connection") 
StrConn = Session("ConnecttoDB") 
myDB.Open StrConn


Here's one way of doing it:

I'm guessing that your classes for your web page codebehind files inheit the Page class. Create a new class file in your ASP.net project that inherits Page. Call it JorgePage. Then, make your codebehind file classes inherit JorgePage.

In JorgePage, write two functions:

private string getUsersDomain()
{
      // returns the user's domain
}

protected string getUsersConnectionString()
{
    switch (getUsersDomain().ToUpper())
    {
        case "ABC":
        return Application("ConnecttoDB_ABC");
        break;
        case "xYZ":
        return Application("ConnecttoDB_XYZ");
        break;
    }
}

Now, the function getUsersConnectionString() is available in the context of all your pages and returns the correct connection string. Furthermore, you have the code in only one place, so if you need to change the logic later, you can do so easily.


Given that you're using classic ASP, you can define a function to return the appropriate connection string in another .asp file and use the #include directive to add it to all your pages.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号