简体   繁体   English

App.config不起作用?

[英]App.config does not work?

i have my App.Config Like this : 我有我的App.Config像这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

 <connectionStrings>
    <add 
      name="MyProject.Properties.Settings.Default.dataConnection"
      connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=address;
              Integrated Security=True;Connect Timeout=30;User Instance=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

and when i want to use following code to get connection string , "Error 1 'Weather.Properties.Settings' does not contain a definition for 'dataConnection' and no extension method 'dataConnection' accepting a first argument of type 'Weather.Properties.Settings' could be found (are you missing a using directive or an assembly reference?) " error rises , 当我想使用以下代码获取连接字符串时,“错误1'Weather.Properties.Settings'不包含'dataConnection'的定义,并且没有扩展方法'dataConnection'接受类型为'Weather.Properties的第一个参数。可以找到“设置”(您是否缺少using指令或程序集引用?)“错误上升,

string connectionString = 
   global::MyProject.Properties.Settings.Default.dataConnection;

To retrieve a connection string from the config file you could use the ConnectionStrings property: 要从配置文件中检索连接字符串,可以使用ConnectionStrings属性:

string connectionString = ConfigurationManager
    .ConnectionStrings["MyProject.Properties.Settings.Default.dataConnection"]
    .ConnectionString;

I notice that the error message has this namespace: 我注意到错误消息具有以下名称空间:

"Weather.Properties.Settings"

While you are referencing 当您参考时

"MyProject.Properties.Settings"

Check that your namespaces are the same. 检查您的名称空间是否相同。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM