简体   繁体   中英

ASP.NET manage multiple DB connections

I didn't find any useful information about this, so I will try here.

My approach is, to have different db connections on my test server and on my life server.

Right now I have one connection String

<connectionStrings>
    <clear/>
    <add name="my_life" ... />
  </connectionStrings>

Important note I had to provide the name of my string to the AspNetSqlMembershipProvider and AspNetSqlRoleProvider

<roleManager>
      <providers>
        <remove name="AspNetSqlRoleProvider"/>
        <add name="AspNetSqlRoleProvider" 
             connectionStringName="my_life" ... />
         </providers>
    </roleManager>

Is there a way to handle the db connection via env variable or maybe url?

I really prefer to use DB Aliases if you want only ip address of your db to be diferent. If you want to have different web.config with different configurations by environment you can use web.config transformations.

Check Web.config Transformation Syntax for Web Application Project Deployment

you can maintain multiple connection strings in a XML file with unique reference id and read this file to get the connection string dynamically in your code where ever you want.

Sample like

<ConnectionString>
<Connection>
<Id></Id>
<ConString></ConString>
</Connection>
</ConectionString>

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