简体   繁体   English

如何在Web.Config中的2个连接字符串之间切换(为DBML激活一个)

[英]How can I switch between 2 Connection Strings in my Web.Config (Activate one for DBML)

I have two connection strings (both in Web.Config: CS_Local and CS_Production) for my DBML (Linq to SQL). 我的DBML(Linq to SQL)有两个连接字符串(在Web.Config中:CS_Local和CS_Production)。

In my Global.Asax/Application_Start I run some production preparation methods if the request is non-local (!HttpContext.Current.Request.IsLocal). 如果请求不是本地请求(!HttpContext.Current.Request.IsLocal),则在Global.Asax / Application_Start中运行一些生产准备方法。 Within that part, I'd also like to change the current connection string used by my DBML from the standard CS_Local to CS_Production. 在那部分中,我还想将DBML使用的当前连接字符串从标准CS_Local更改为CS_Production。

How would I do that? 我该怎么做? Some help please.. 请一些帮助..

You can define the dbml context on the fly with: 您可以使用以下方法动态定义dbml上下文:

string connectionString = HttpContext.Current.Request.IsLocal ? 
    ConfigurationManager.ConnectionStrings["CS_Local"].ConnectionString :
    ConfigurationManager.ConnectionStrings["CS_Production"].ConnectionString;
yourDataContext = new YourApplicationDataContext(connectionString);

暂无
暂无

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

相关问题 我们如何在 Web.Config 中有两个连接字符串并在后面的代码中在它们之间切换? - How Can We Have two Connection Strings In Web.Config And Switch Betweeen Them In Code Behind? 我可以通过Web.Config连接字符串优化数据库连接吗? - Can I optimize my database connections via Web.Config connection strings? 如何在连接字符串上使用Web.Config转换? - How do I use Web.Config transform on my connection strings? 在C#中,如何读取存储在web.config文件连接字符串中的连接字符串? - In C# , how can I read a connection string stored in my web.config file connection string? 如何通过Windows应用程序访问Web.Config文件中的连接字符串 - How can I access the connection string in my Web.Config file through a Windows Application 如何更改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 我在web.config中需要两个连接字符串吗? - Do I need two connection strings in web.config? 在VS 2005中,如何为web.config生成连接字符串? - In VS 2005 How do I generate the connection strings for web.config? 如何在web.config之外的ASP.Net中保留连接字符串? - How can I persist a connection string in ASP.Net outside of web.config? web.config 到一个 SQL DB 的两个连接字符串在调试中工作,但在发布时不起作用 - Two connection strings in web.config to one SQL DB work in debug but not when published
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM