当前位置:数据库 > Access >>

asp.net连接access数据库路径问题最终解决方案

解决方案一:
在 Web.Config 中配置 Access 数据库教程驱动和数据库文件名称。
请看代码

<appSettings>
<add key="DBDriver" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source ="/>
<add key="DBName" value="Company.mdb"/>
</appSettings>

在数据库访问层,如 OleDBHelper.cs 中获得 Access 数据库链接字符串。

/**//// <summary>
/// 从Web.Config取得数据库联接字符串
/// </summary>
//从配置文件中得到数据库名称
public static readonly string DBName = ConfigurationManager.AppSettings.Get("DBName").ToString();
//从配置文件中得到数据库驱动
public static readonly string DBDriver = ConfigurationManager.AppSettings.Get("DBDriver").ToString();
//得到数据库连接字符串
private static string DBConnectionString = DBDriver + HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/App_Data/") + DBName;
//建立数据库连接对象
private static OleDbConnection OleDbConn = new OleDbConnection(DBConnectionString);

这样设置后,无论在任何子目录,都能通过以上代码正确的访问数据库。


解决方案二:

<appSettings>
<add key="SQLConnString" value="provider=microsoft.jet.oledb.4.0;data source="/>
<add key="dbPath" value="~/App_Data/mydata.mdb"/>
</appSettings>

程序中的数据访问类中我把"SQLConnString"和"dbPath"取出来连接成一个字符串"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"]) + ";";
补充:数据库,Access 
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,