简体   繁体   中英

How can I change connection strings for database in my web.config that have database name based on Login_User table from LOGIN database

Currently I have one connection string in my web.config that has database: NORTHWIND_HR - like this:

<connectionStrings>
    <add name="MyConectionString" 
         connectionString="Data Source=127.0.0.1;Initial Catalog=NORTHWIND_HR;User ID=userName;Password=PASS" />
</connectionStrings>`

I have many grids, table, dropdown, based on that connection string in my web applications. So all the controls that require something from the database use SqlDataSource and the same connection string.

<asp:SqlDataSource id="SqlDataSource1" runat="server"
     ConnectionString="<%$ ConnectionStrings:MyConectionString %>" 
     ProviderName="System.Data.SqlClient" 
     SelectCommand="SELECT ... ">
</asp:SqlDataSource>

Up until now I only needed one connection string since I had only one database but this has changed since I want different users group to login to different database based on departments for which I have crated one login database that have user and password and database names.

NOTE : these are demo structures for explaining my requirements only.

登录数据库

As per requirement's I will have to create different database (approx 100000) dynamically with same schema and table.

具有相同架构和表的数据库

This application is totally based on .aspx file, I had don't touch the code behind .aspx.cs file because as per my requirements, the website will be ALWAYS ON (like SQL Server 2014 always on features), and the website and database will be modified for 2 years in real time on server. I don't want to stop the website for upload new .dll file, that's why I used SqlDataSource .

<connectionStrings>
    <add name="MyConectionString" 
         connectionString="Data Source=127.0.0.1;Initial Catalog=NORTHWIND_HR;User ID=userName;Password=PASS" />
</connectionStrings>`

How can I change connection strings DATABASE in my web.config that have database based on Login_User table from LOGIN database?

Or is there any other technique to define connection strings at any other location?

Or is there any other technique?

Your app would need 1 connection string per database, and when you attempt to establish the connection to the database pull the correct connection string per file. So the data access layer switches permission by role. Ideally, you'd have to have a master database for the users with the appropriate database they should connect to.

Rather than having database separation, I would recommend having the separation defined within the application (one database, with application role security defined within the application). There is complexity, sure, but it will be easier to maintain over time.

I) If you have access to the details of the currently logged in user in your asp code, the easiest solution would be:

1) Add the connection strings in the web.config

<connectionStrings>
    <add name="MyConectionString_IT" connectionString="Data source=127.0.0.1;Initial Catalog=NORTHWIND_IT;User ID=userName;Password=PASS" />
    <add name="MyConectionString_HR" connectionString="Data Source=127.0.0.1;Initial Catalog=NORTHWIND_HR;User ID=userName;Password=PASS" />
</connectionStrings>

2) In the ASP code, check using an IF statement to see what DATABASE the user has assigned and create the control element based on that. Something like:

<% IF user.Database = "NORTHWIND_IT" Then
    <asp:SqlDataSource id="SqlDataSource1" runat="server"
         ConnectionString="<%$ ConnectionStrings:MyConectionString_IT %>" 
         ProviderName="System.Data.SqlClient" 
         SelectCommand="SELECT ... ">
    </asp:SqlDataSource>

    ELSE

    <asp:SqlDataSource id="SqlDataSource1" runat="server"
         ConnectionString="<%$ ConnectionStrings:MyConectionString_HR %>" 
         ProviderName="System.Data.SqlClient" 
         SelectCommand="SELECT ... ">
    </asp:SqlDataSource>

    END IF
    %>

II) If the username, password and data source will be the same for all the databases, you can use string format to replace the initial catalog with the database you want.

<connectionStrings>
    <add name="MyConectionString" connectionString="Data source=127.0.0.1;Initial Catalog={0};User ID=userName;Password=PASS" />
</connectionStrings>

and your ASP code:

<asp:SqlDataSource id="SqlDataSource1" runat="server"
     ConnectionString="<%$ String.Format(ConnectionStrings:MyConectionString, user.Database) %>" 
     ProviderName="System.Data.SqlClient" 
     SelectCommand="SELECT ... ">
</asp:SqlDataSource>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM