Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在运行时更改DbEntry设置_C#_Asp.net_Mysql - Fatal编程技术网

C# 如何在运行时更改DbEntry设置

C# 如何在运行时更改DbEntry设置,c#,asp.net,mysql,C#,Asp.net,Mysql,我想根据应用程序的运行位置,更改存储在Web.config文件中的DB字符串连接 我知道如何阅读,但有人知道如何改变它吗? 第三个链接将出现在一个页面上 新答案基于您对我的原始答案的评论 您可以使用String.Replace函数。ConnectionString只是一个字符串 string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["YourConnectionString"].

我想根据应用程序的运行位置,更改存储在Web.config文件中的DB字符串连接

我知道如何阅读,但有人知道如何改变它吗?

第三个链接将出现在一个页面上

新答案基于您对我的原始答案的评论

您可以使用String.Replace函数。ConnectionString只是一个字符串

string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
connectionString = connectionString.Replace("TextToReplace", "ReplacementText");
假设你的连接字符串是

"Data Source=LiveDbServer;Initial Catalog=MyDataBase;Persist Security Info=True;User ID=someUserId;Password=yourPassword"
您可以使用以下逻辑

if(System.Environment.ServerName == "MyTestServer")
{
   connectionString = connectionString.Replace("LiveDbServer", "TestDbServer");
}

以下只是基于猜测,您可能希望在运行时根据应用程序运行的服务器更改ConnectionString

尽管(增加了额外的想法)如果您的目标是始终让您的测试web服务器指向测试DB服务器,并且让您的live web服务器指向live DB服务器,那么一个更简单的方法是。(我确信还有其他选择,但这对我们来说是一个足够简单的黑客攻击,它可以用于数据库连接、web服务引用等。)


只需在测试机器上设置hosts文件,使其将“MyLiveDbServer”映射到“MyTestDbServer”的ipaddress,viola-您的应用程序将始终自动指向测试服务器,而无需任何额外代码或配置。

我曾将连接字符串加载到应用程序对象中(无论好坏)在global.asax的应用程序启动事件中

然后,我可以选择检查正在运行的服务器并将该连接字符串加载到应用程序对象中,而不是仅仅依赖web.config中的一个条目

例如:

<connectionStrings>
  <add name="testservername" connectionString="..." providerName="..."/>
  <add name="prodservername" connectionString="..." providerName="..."/>
  <add name="localhost"      connectionString="..." providerName="..."/>
</connectionStrings>

…在global.asax中:

<%@ Import Namespace="System.Configuration.ConfigurationManager" %>
...
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
  Application("cn") = ConnectionStrings(System.Web.HttpContext.Current.Request.ServerVariables("HTTP_HOST"))
End Sub

...
子应用程序_启动(ByVal发送方作为对象,ByVal e作为事件参数)
应用程序(“cn”)=连接字符串(System.Web.HttpContext.Current.Request.ServerVariables(“HTTP\U主机”))
端接头
因此,每次我使用
连接字符串(“cn”)
,我都会使用
应用程序(“cn”)

检查HTTP_主机在每台服务器上为您提供了什么,并相应地命名您的连接字符串

或者,您可以只使用
连接字符串(System.Web.HttpContext.Current.Request.ServerVariables(“HTTP\u主机”)
而不是将整个字符串存储在应用程序中


另一种选择是只将主机名存储在应用程序变量中,以便在需要时随时传递给
ConnectionStrings

如果要在宿主服务器中更改连接字符串,可以通过在记事本中打开来更改web.config中的连接字符串。是的,但我正在尝试在运行时(以编程方式)配置DbEntry。换句话说,是否有其他方法(以编程方式)在不使用web.config文件的情况下配置DbEntry?
<%@ Import Namespace="System.Configuration.ConfigurationManager" %>
...
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
  Application("cn") = ConnectionStrings(System.Web.HttpContext.Current.Request.ServerVariables("HTTP_HOST"))
End Sub