怎么获取MVC Web.config 内的连接字符串
2个回答
展开全部
ASP.NET MVC 4中读取web.config中的配置方法如下:
web.config内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation defaultLanguage="c#" debug="true" />
</system.web>
<appSettings>
<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>
</configuration>
读取配置的方法:
void Page_Load(object sender, EventArgs e)
{
string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];
using(SqlConnection connection = new SqlConnection(connectionInfo))
{
connection.Open();
// perform work with connection
}
}
web.config内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation defaultLanguage="c#" debug="true" />
</system.web>
<appSettings>
<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>
</configuration>
读取配置的方法:
void Page_Load(object sender, EventArgs e)
{
string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];
using(SqlConnection connection = new SqlConnection(connectionInfo))
{
connection.Open();
// perform work with connection
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static class ConfigHelper
{
public static readonly string ConnStrScO2O = ConfigurationManager.ConnectionStrings["ScO2O"].ConnectionString;
}
要引用dll还有添加命名空间
view中的webconfig是
通常添加一些公共的命名空间,这样就不用每个view都独自using了
{
public static readonly string ConnStrScO2O = ConfigurationManager.ConnectionStrings["ScO2O"].ConnectionString;
}
要引用dll还有添加命名空间
view中的webconfig是
通常添加一些公共的命名空间,这样就不用每个view都独自using了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询