简体   繁体   English

如何更改Web.config中数据库的连接字符串,这些字符串具有基于LOGIN数据库的Login_User表的数据库名称

[英]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: 目前,我的web.config中有一个连接字符串,该字符串具有database: NORTHWIND_HR像这样:

<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. 基于Web应用程序中的连接字符串,我有很多网格,表格,下拉列表。 So all the controls that require something from the database use SqlDataSource and the same connection string. 因此,所有需要数据库中的内容的控件都使用SqlDataSource和相同的连接字符串。

<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. 根据要求,我将必须使用相同的架构和表动态创建不同的数据库(大约100000)

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

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. 此应用程序完全基于.aspx文件,我没有碰过.aspx.cs文件后面的代码,因为根据我的要求,该网站将始终处于打开状态(如SQL Server 2014始终具有功能),并且该网站和数据库将在服务器上实时修改2年。 I don't want to stop the website for upload new .dll file, that's why I used SqlDataSource . 我不想停止网站上传新的.dll文件,这就是为什么我使用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? 如何在我的web.config中更改具有基于LOGIN数据库中基于Login_User表的数据库的连接字符串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. 您的应用程序每个数据库需要1个连接字符串,并且当您尝试建立与数据库的连接时,请为每个文件提取正确的连接字符串。 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: I)如果您可以在asp代码中访问当前登录用户的详细信息,最简单的解决方案是:

1) Add the connection strings in the web.config 1)在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. 2)在ASP代码中,使用IF语句检查用户已分配的数据库,并基于该数据库创建控制元素。 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. II)如果所有数据库的用户名,密码和数据源都相同,则可以使用字符串格式将初始目录替换为所需的数据库。

<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代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以通过Web.Config连接字符串优化数据库连接吗? - Can I optimize my database connections via Web.Config connection strings? 来自其他项目中web.config的数据库连接 - Database connection from web.config in other project 在Web.config中更改连接字符串以访问本地数据库而不是联机数据库 - Changing Connection Strings in Web.config to access local database instead of online database 我正在尝试通过web.config文件建立与数据库的连接-只是不起作用 - I'm trying to set up a connection to my database via the web.config file - It's just not working 如何在Web.Config中的2个连接字符串之间切换(为DBML激活一个) - How can I switch between 2 Connection Strings in my Web.Config (Activate one for DBML) web.config asp.net中的oracle数据库连接 - oracle database connection in web.config asp.net web.config中连接SQL Server数据库的连接字符串是什么? - What is the connection String to connect to the SQL Server database in web.config? 数据库连接:Web.config Asp.net - Database Connection: Web.config Asp.net 在ASP.Net Web.config中加密数据库连接字符串 - Encrypt database connection string in ASP.Net Web.config 在web.config中使用数据库连接来填充下拉列表 - Using database connection in web.config to populate dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM