找不到连接字符串我尝试了多种方法,但都顺其自然,我在app.config中有一个数据访问层项目我提到了连接字符串

 </configuration>
     <connectionStrings>
        <add name="MySqlConnectionString" connectionString="server=myserver;User Id=myuser;password=mypassword;database=mydatabase"
     providerName="MySql.Data.MySqlClient" />
      </connectionStrings>
    </configuration>


C#代码

public void ConnectionString()
{
    try
    {
        var connectionString = ConfigurationManager.ConnectionStrings["MySqlConnectionString"].ConnectionString; //does not work
        var connectionString2 = ConfigurationManager.AppSettings["MySqlConnectionString"];//does not work
    }
    catch(Exception ex)
    {

    }
}


我做错了什么我尝试了多种解决方案对我不起作用

ConnectionString Not Found

最佳答案

谢谢大家帮助我的主要问题是我在错误的地方添加了连接字符串属性

错误的地方

 <runtime>
    <connectionStrings>
      <add name="MySqlConnectionString" connectionString="server=myserver;User Id=myuser;password=mypassword;database=mydatabse"
   providerName="MySql.Data.MySqlClient" />
    </connectionStrings>

  </runtime>
</configuration>


正确的地方,我在运行时属性下剪切了连接字符串,并将其单独粘贴并解决了问题

<connectionStrings>
          <add name="MySqlConnectionString" connectionString="server=myserver;User Id=myuser;password=mypassword;database=mydatabse"
       providerName="MySql.Data.MySqlClient" />
        </connectionStrings>


感谢大家

10-07 21:46