急!ASP.NET中多次用到和数据库连接的字符串,怎么在Web.config文件中定义一个公共的值,以后可直接调用?

2024-11-30 17:36:49
推荐回答(3个)
回答1:

ACCESS数据库的话,写法为:




引用为:
public partial class _Default : System.Web.UI.Page
{
private static string connString = String.Format(ConfigurationSettings.AppSettings["AccessConnStr"], HttpContext.Current.Server.MapPath("../"));//如果是同目录,用MapPath(".")下级子目录用../
protected void Page_Load(object sender, EventArgs e)
{

}
}

另:在CONFIG文件中,是不支持Server.MapPath的,因为那就是一个XML文件,本身是不能运算的。

回答2:







之后在程序里可以用System.Configuration.ConfigurationManager.AppSettings["P"]取值

回答3:

在Web.config中有两种方式:
一,写到节点中

providerName="System.Data.SqlClient"/>

二,




在程序里引用都是一样的:
string connStr = System.Configuration.ConfigurationManager.AppSettings["conn"];