从App.config获取连接字符串并在Linq dataContext中使用它

时间:2011-01-13 17:58:02

标签: .net data-binding configuration connection-string system.configuration

我应该如何提取以下连接字符串并将其应用于我的Linq上下文?我知道Linq部分,但是System.Configuration部分很弱。

  <connectionStrings>
        <!-- Production

        <add name="Management_Report_Emailer.Properties.Settings.DWConnectionString"
            connectionString="Data Source=prodSQL01;Initial Catalog=DW;Integrated Security=True"
            providerName="System.Data.SqlClient" />-->


      <!-- QA-->

      <add name="Management_Report_Emailer.Properties.Settings.DWConnectionString"
            connectionString="Data Source=qaSQL01;Initial Catalog=DW;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

2 个答案:

答案 0 :(得分:2)

ConfigurationManager.ConnectionStrings("Management_Report_Emailer.Properties.Settings.DWConnectionString").ConnectionString

答案 1 :(得分:0)

ConfigurationManager.ConnectionStrings["Management_Report_Emailer.Properties.Settings.DWConnectionString"].ConnectionString;