三层架构(仿petshop),在DAL中如何取得web.config中连接语句?
DAL中有个GetConnection.cs,其中有这样一句:
public static string GetConnectionString()
{
return "data source=. ;initial catalog=Test ;persist security info=False;user id=book;password=123456;workstation id=AIDE-7CE47A35A7;packet size=4096; ";
}
但是里面的值我想从web.config 中读取
<connectionStrings>
<add name= "mytest " connectionString= "Data Source=.;Initial Catalog=Test;User ID=book;Password=123456 "
providerName= "System.Data.SqlClient " />
</connectionStrings>
这 public static string GetConnectionString()里面的语句应该怎么写呢?新手刚学,高手请勿笑话.好像用
return ConfigurationManager.ConnectionStrings[ "mytest "].ConnectionString;不行吧.
------解决方案--------------------怎么不行了?
------解决方案--------------------就是这么写啊
------解决方案--------------------using System.Configuration;
------解决方案--------------------public static string connStr = ConfigurationManager.ConnectionStrings[ "SQLConnString "].ConnectionString;
------解决方案--------------------public static string GetConnectionString
{
get
{
return System.Configuration.ConfigurationManager.ConnectionStrings[ "mytest "].ConnectionString;
}
}
------解决方案--------------------楼上这样就行了!!
------解决方案--------------------ConfigurationManager.ConnectionStrings[ "SQLConnString "].ConnectionString;
------解决方案--------------------在项目浏览器上点 右键 点添加饮用 System.Configuration
这个
------解决方案--------------------引用命名空间
------解决方案--------------------输完return 后按空格,输入C,然后alt+右箭头,