解決方案一:
在 Web.Config 中配置 Access 數(shù)據(jù)庫教程驅(qū)動(dòng)和數(shù)據(jù)庫文件名稱。
請(qǐng)看代碼
<appSettings>
<add key="DBDriver" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source ="/>
<add key="DBName" value="Company.mdb"/>
</appSettings>
在數(shù)據(jù)庫訪問層,如 OleDBHelper.cs 中獲得 Access 數(shù)據(jù)庫鏈接字符串。
/**//// <summary>
/// 從Web.Config取得數(shù)據(jù)庫聯(lián)接字符串
/// </summary>
//從配置文件中得到數(shù)據(jù)庫名稱
public static readonly string DBName = ConfigurationManager.AppSettings.Get("DBName").ToString();
//從配置文件中得到數(shù)據(jù)庫驅(qū)動(dòng)
public static readonly string DBDriver = ConfigurationManager.AppSettings.Get("DBDriver").ToString();
//得到數(shù)據(jù)庫連接字符串
private static string DBConnectionString = DBDriver + HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/App_Data/") + DBName;
//建立數(shù)據(jù)庫連接對(duì)象
private static OleDbConnection OleDbConn = new OleDbConnection(DBConnectionString);
這樣設(shè)置后,無論在任何子目錄,都能通過以上代碼正確的訪問數(shù)據(jù)庫。
解決方案二:
<appSettings>
<add key="SQLConnString" value="provider=microsoft.jet.oledb.4.0;data source="/>
<add key="dbPath" value="~/App_Data/mydata.mdb"/>
</appSettings>
程序中的數(shù)據(jù)訪問類中我把"SQLConnString"和"dbPath"取出來連接成一個(gè)字符串"CONN_STRING_NON_DTC"
public static readonly string CONN_STRING_NON_DTC = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString() + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"]) + ";";
更多信息請(qǐng)查看IT技術(shù)專欄