ConfigurationManager.Connectionstrings(" DBConnection") - 空例外

时间:2016-12-08 12:10:51

标签: asp.net vb.net connection-string app-config

在数据层中,我试图从app.config中获取连接字符串。

的App.config

<connectionStrings>
   <add name="DBConnection" connectionString="Data Source=****;Initial Catalog=****;Persist Security Info=True;User ID=*****; Password=***" />
  </connectionStrings>

获取价值:

Shared _ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString

这不起作用。

我试过

Shared _ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings(0).ConnectionString

它是部分提取 - &#34;数据源= ****;初始目录= ****;持久安全信息=真&#34;

请解释一下。

1 个答案:

答案 0 :(得分:-3)

尝试使用ConnectionStrings["DBConnection"].ConnectionString代替ConnectionStrings("DBConnection").ConnectionString