asp.net連接access數(shù)據(jù)庫(kù)路徑問(wèn)題

字號(hào):


    解決方案一:
    在 web.config 中配置 access 數(shù)據(jù)庫(kù)教程驅(qū)動(dòng)和數(shù)據(jù)庫(kù)文件名稱。
    請(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ù)名稱
    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)類中我把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]) + ;;