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