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

字號:


    解決方案一:
    在 web.config 中配置 access 數(shù)據(jù)庫教程驅(qū)動和數(shù)據(jù)庫文件名稱。
    請看代碼
    <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ū)動
    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ù)庫連接對象
    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]) + ;;