在web.config中使用象asp那樣的Server.MapPath取數(shù)據(jù)庫路徑
但web.config不認識Server.MapPath,此方法致使程序無法調試,行不通。
如下方法,可以方便的移植程序路徑而不必再去修改access數(shù)據(jù)庫路徑。
在web.config中的寫法如下:
<appSettings>
<add key="ConnStr" value="PRovider=microsoft.jet.oledb.4.0;data source="/>
<add key="dbPath" value="/AppData/mydata.mdb"/>
</appSettings>
程序中的數(shù)據(jù)訪問類中我把"ConnStr"和"dbPath"取出來再連接成一個字符串
代碼如下:
/// <summary>
/// (靜態(tài))返回數(shù)據(jù)庫連接串。
/// </summary>
protected static string GetConnString()
{
return
System.Configuration.ConfigurationSettings.AppSettings["ConnStr"]+ System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["DataPath"])+";";
}
新聞熱點
疑難解答