C#配置管理器。的ConnectionStrings [英] C# Configuration Manager . ConnectionStrings

查看:794
本文介绍了C#配置管理器。的ConnectionStrings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含包含一个连接字符串,应用程序配置文件控制台应用程序如下图所示:

I have a console app containing an application configuration file containing one connection string as shown below:

<configuration>
  <connectionStrings>
    <add name="Target" 
      connectionString="server=MYSERVER; Database=MYDB; Integrated Security=SSPI;" />
  </connectionStrings>
</configuration>

当我使用它传递给我的连接方式:

When I pass this to my Connection using:

ConfigurationManager.ConnectionStrings[1].ToString()

我有两个值在那里,因此使用集合中的第二个,我的问题是,什么是第二次来的?

I have two values in there, hence using the second in the collection, my question is where is this second coming from?

我已经检查了\\ BIN的版本和原来和我无关!它显然是一个系统生成的一个,但我还没有见过这个?任何人都可以告诉我吗?

I have checked the \Bin version and original and its not mine! Its obviously a system generated one but I have not seen this before? Can anyone enlighten me?

神秘的连接字符串是:

data source=.\SQLEXPRESS;
          Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;
          User Instance=true

这是不是一个问题,因为这样我只是想知道这是为什么出现?在此先感谢!

This isn't a problem as such I would just like to know why this is occuring? Thanks in advance!

有关备查那些谁可能会或可能不会在这个绊倒,发现了的machine.config ,它变得很明显这是不好的做法,通过指一个配置后,其作为每个堆栈索引将可能是不同的,这就是为什么密钥被使用。

For future reference to those who may or may not stumble on this, after discovering the machine.config, it's become apparent it is bad practice to refer to a config by its index as each stack will potentially be different, which is why "Keys" are used.

在这种情况下我的code将是:

In this instance my code would be:

ConfigurationManager.ConnectionStrings["Target"].ToString()

干杯所有!

推荐答案

检查的machine.config 。如果你只想要你进入,你可以添加一个&LT;清/&GT; 元素添加到&LT;是connectionStrings&GT; 像这样的元素...

Check your machine.config. If you only want your entry, you can add a <clear /> element to the <connectionStrings> element like so ...

<connectionStrings>
  <clear />
  <add name="Target" 
       connectionString=
             "server=MYSERVER; Database=MYDB; Integrated Security=SSPI;" />
</connectionStrings>

这篇关于C#配置管理器。的ConnectionStrings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆