简体   繁体   English

如何连接到不同的数据库?

[英]How to connect to different databases?

I'm currently developing an application based on ASP.NET MVC3, SQL Server 2008 and EF with database first. 我目前正在开发基于ASP.NET MVC3,SQL Server 2008和EF的应用程序,首先是数据库。

My application requires every user to have it's own SQL Server database. 我的应用程序要求每个用户拥有自己的SQL Server数据库。 These databases all have an identical structure. 这些数据库都具有相同的结构。

My customers are identified with a customercode , eg fgt . 我的客户被识别为customercode ,例如fgt This code is provided in the url. 此代码在网址中提供。 I need to know how can I retrieve the customercode from the url and set the connection string accordingly. 我需要知道如何从url中检索customercode并相应地设置连接字符串。

Thanks for the help 谢谢您的帮助


My idea is to connect to the database once the customercode is retrieved from the URL and then prompt to user to enter his username and password for access data. 我的想法是,一旦从URL检索到客户代码,就连接到数据库,然后提示用户输入访问数据的用户名和密码。 But yes, is a good idea to create a database to store the connection string of each customer. 但是,是的,创建一个存储每个客户的连接字符串的数据库是个好主意。 Can anyone write the code that I need for do this please?. 任何人都可以编写我需要的代码吗? I am new to asp. 我是asp新手。 I come from php. 我来自php。

(I'm just learning English. Sorry about the bad grammar) (我刚刚学习英语。抱歉语法不好)

Try something like this to get started: 尝试这样的事情开始:

string customerCode = Request.QueryString["cust"].ToString();

string myNewConnString = ConfigurationManager
                           .ConnectionStrings["MyDatabase"]
                           .ConnectionString
                           .Replace("[placeholder]", customerCode);

Where your connection string in your .config is something like this. .config中的连接字符串就是这样的。 Note that I've assumed you'll place a token to be replaced ( [placeholder] ). 请注意,我假设您将放置一个要替换的令牌( [placeholder] )。

 <add name="MyDatabase" 
      connectionString="Data Source=192.168.0.1;Initial Catalog=[placeholder];User ID=foo;Password=bar"     
      providerName="System.Data.SqlClient" />

Suggest that you whitelist your customers and their connection strings. 建议您将客户及其连接字符串列入白名单。

  • setup a web service on your side. 设置您的Web服务。
  • your deployed application calls your web service using the customer code. 部署的应用程序使用客户代码调用Web服务。
  • web service validates the customer code, and returns a valid conn string. Web服务验证客户代码,并返回有效的conn字符串。
  • customer's app keeps the conn string in memory (session, cache, whathaveyou). 客户的应用程序将conn字符串保存在内存中(session,cache,whathaveyou)。

This would allow you to ensure the conn string is always valid for a given customer code. 这将允许您确保conn字符串始终对给定的客户代码有效。 You'd have fine grain control on access to the database for any reason (non-payment, etc). 出于任何原因(非付款等),您可以对数据库进行精细控制。 Your service would have a few hits thanks to any caching that you build in. 由于您构建的任何缓存,您的服务会有一些点击。

maybe sqlshard could help you in using multiple databases? 也许sqlshard可以帮助你使用多个数据库?

http://enzosqlshard.codeplex.com/ http://enzosqlshard.codeplex.com/

Sounds like pretty insecure solution. 听起来很不安全的解决方案。 What if customer put another customer code in URL? 如果客户在URL中放入另一个客户代码怎么办? Where is validated if customer can access that code if you don't have any central database with customer's permissions? 如果您没有任何具有客户权限的中央数据库,客户是否可以访问该代码?

You need authentication and some central store (additional database) where you will validate that user accessing the application has access permissions to provided URL. 您需要身份验证和一些中央存储(附加数据库),您将验证访问该应用程序的用户是否具有对提供的URL的访问权限。 It will also validate if such database even exists. 它还将验证此类数据库是否存在。 Next you need just SqlConnectionStringBuilder and use the customer code as a name of database (or part of the name). 接下来,您只需要SqlConnectionStringBuilder并使用客户代码作为数据库的名称(或名称的一部分)。 For security reason each database should have a separate SQL account with permissions to read only from that database. 出于安全原因,每个数据库应该具有单独的SQL帐户,该帐户具有仅从该数据库读取的权限。 This db account can also be stored with that central storage with encrypted passwords. 此db帐户还可以与具有加密密码的中央存储一起存储。

There can be additional complexities if you also expect dynamical adding or removing customer codes = databases. 如果您还期望动态添加或删除客户代码=数据库,则可能存在其他复杂性。 That would require high privileged account to manage logins, accounts, databases, etc. 这将需要高特权帐户来管理登录,帐户,数据库等。

Because you are asking how to get part of Uri it looks like you have almost no experience with ASP.NET MVC and perhaps with everything related. 因为您正在询问如何获得Uri的一部分,所以看起来您几乎没有使用ASP.NET MVC的经验,也许还有与之相关的一切。 You should definitely ask any more skilled colleague or boss to review your architecture because at this point it looks like you are going to have serious problems and you can develop very insecure application which can significantly harm reputation of your company and your customer. 你肯定应该让任何更熟练的同事或老板审查你的架构,因为此时看起来你会遇到严重的问题而且你可以开发非常不安全的应用程序,这会严重损害你公司和客户的声誉。

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

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