我的web.config文件如下. 大家帮看看, 在线等待
<configuration>
<configSections>
</configSections> <connectionStrings>
<add name="SqlConnString" connectionString="Data Source=server1;Initial Catalog=hrm;Persist Security Info=True;User ID=edi;Password=pwdedi" providerName="System.Data.SqlClient"/> </connectionStrings>
<system.web>
</system.web>
<system.webServer>
</system.webServer>
<location allowOverride="true" inheritInChildApplications="true">
<appSettings>
</appSettings>
</location>
</configuration>
<configuration>
<configSections>
</configSections> <connectionStrings>
<add name="SqlConnString" connectionString="Data Source=server1;Initial Catalog=hrm;Persist Security Info=True;User ID=edi;Password=pwdedi" providerName="System.Data.SqlClient"/> </connectionStrings>
<system.web>
</system.web>
<system.webServer>
</system.webServer>
<location allowOverride="true" inheritInChildApplications="true">
<appSettings>
</appSettings>
</location>
</configuration>
跟在页面使用没什么区别
然后在项目上右键“添加引用”,找到System.Configuration并添加;
再使用ConfigurationManager.ConnectionStrings["SqlConnString"].ConnectionString.ToString();
SQLHelper:
using System.Web.Configuration;
public static string getConnStr()
{
return ConfigurationManager.ConnectionStrings["SqlConnString"].ToString();
}
DAL
SQLHelper.getConnStr();
http://www.cnblogs.com/insus/articles/1405604.html
然后添加引用System.Configuration
这样就可以使用ConfigurationManager.ConnectionStrings["connStr"].ConnectionString.ToString()