C# 如何从 app.config 访问 VS2012 WPF 应用程序中的连接字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16144253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:07:15  来源:igfitidea点击:

How to access connection string in VS2012 WPF application from app.config?

c#.net

提问by

I am using VS2012. I have to keep connection string in app.config and have to access it from my cs file. But I am unable to do it in VS2012. Following is what I have found from net but I think it works on earlier version of VS not on VS2012.

我正在使用 VS2012。我必须在 app.config 中保留连接字符串,并且必须从我的 cs 文件中访问它。但我无法在 VS2012 中做到这一点。以下是我从网上找到的内容,但我认为它适用于早期版本的 VS,而不适用于 VS2012。

app.config file

app.config 文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
   <connectionStrings>
      <add name="DataFormConnection"
            connectionString="Data Source=abcdd;database=xyz;uid=4566;pwd=987"
            providerName="System.Data.SqlClient" />
   </connectionStrings>
</configuration>

How I am accessing it:

我如何访问它:

connectionString = ConfigurationManager.ConnectionStrings["DataFormConnection"].ConnectionString;

Getting error: type or name does not exist in System.Configuration.ConfiguarationSettings

获取错误:System.Configuration.ConfiguarationSettings 中不存在类型或名称

采纳答案by misleadingTitle

Go to references, and add a reference to System.Configuration.

转到引用,并添加对System.Configuration.

回答by Sachin

Go to References, and add a reference to System.Configuration

转到References,并添加对System.Configuration

Once you have done this, you should be able to reference System.Configuration.ConfigurationManager.

完成此操作后,您应该可以参考System.Configuration.ConfigurationManager.