English 中文(简体)
B. 经营时不断变化的连接线体
原标题:changing connection strings at runtime

I have a config file and a pollingservice with different methods in the polling service. What i want to happen is that when it hits a certain method in the polling service i want to change the connection string in the config file from:

<add name="Entity" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=dev;Initial Catalog=DB;Persist Security Info=True;User ID=a;Password=asdf;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

为此:

 <add name="Entity" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=dev_Test;Initial Catalog=DB_Test;Persist Security Info=True;User ID=a;Password=asdf;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

请协助。

感谢。

问题回答

如果你真的想要改变 app子,你可以这样做:

  var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

  config.ConnectionStrings.ConnectionStrings["Entity"].ConnectionString = YourNewConnectionString;
  config.Save();

如果你只需要改变数据来源是内部连接线,而你不想打平实体的连接线,那么你就可以利用连接线铺设建筑商:

var entityBuilder = new EntityConnectionStringBuilder(ConfigurationManager.ConnectionStrings["Entity"].ConnectionString);
var sqlBuilder = new SqlConnectionStringBuilder(entityBuilder.ProviderConnectionString);

sqlBuilder.DataSource = "dev_Test";

entityBuilder.ProviderConnectionString = sqlBuilder.ToString();

var connectionString = entityBuilder.ToString();




相关问题
Feedback service for Push notification

I wrote the below script to read the feedback data. But, for some reasons, I am not receiving any data from the server. Can you kindly let me know whats wrong with the script. Also, if you have any ...

Windows service runs file locally but not on server

I created a simple Windows service in dot net which runs a file. When I run the service locally I see the file running in the task manager just fine. However, when I run the service on the server it ...

采用网路服务方法

我撰写了一个网络服务,期望一个参数(所谓的“hlink”)成为一种ur。 在使用网络服务之前,URLEncode 所涉参数(“hlink”)。 然后我打电话......。

PDF conversion service

I need to develop a service able to convert MS Office and Open Office documents to PDF. And the PDF`s also need to be commentable when opened in ADOBE Reader. I have used a piece of software from www....

热门标签