日期:2014-05-17  浏览次数:20446 次

请问如何在web.config文件中配置ACCESS数据库相对路径?
web.config原配置如下
<appSettings>
<add   key= "connectionaccess1 "value= "provider=Microsoft.JET.OleDb.4.0;Data  

Source=D:angel\\mis\\a\\myweb\\drugbase.mdb "/>


但是这样程序没有可移植性很差,换到其他机器就要改路径,所以需要改为相对路径,但是改成Data   Source=drugbase.mdb,就会报错,说在C:winnt\system32目录下找不到drugbase.mdb,请高手指教怎么能把默认目录改成我的程序根目录?或怎么能在web.config中设置相对路径?我直接在控制台编译输出时用provider=Microsoft.JET.OleDb.4.0;Data   Source=drugbase.mdb是可以通过的。

------解决方案--------------------
Server.Mappth 取出项目所在路径
------解决方案--------------------
Server.MapPath( "虚拟路径名 ");
------解决方案--------------------
你就设置mdb相对应用程序的根目录就可以了
<appSettings>
<add key= "connectionaccess1 " value= "drugbase.mdb "/>

用的时候
str = "provider=Microsoft.JET.OleDb.4.0;Data Source= " + Server.MapPath( "~ ") + Config[ "connectionaccess1取出drugbase.mdb "]


------解决方案--------------------
net_lover(【孟子E章】偶像
------解决方案--------------------
汉,要用相对路径必需要创建了web站点哒昂,通常当然是咱们的C:\Inetpub\wwwroot 如果你只有这一个站点,那么数据库要是在D:angel\mis\a\myweb\drugbase.mdb 那就不能用相对路径了 而Server.Mappth 也是相对于web站点来说哒
------解决方案--------------------
难道你一个应用不是一个WEB站点吗?
------解决方案--------------------
Server.MapPath( "*.mdb ");
------解决方案--------------------
<appSettings>
<add key= "ConnectionString " value= "Provider=Microsoft.Jet.OLEDB.4.0;Password=;User ID=Admin;Data Source={0}\DataBase\Gproject.Config;Mode=Share Deny None;Extended Properties= "/>
</appSettings>

protected static string connectionString = string.Format(ConfigurationSettings.AppSettings[ "ConnectionString "],System.Web.HttpContext.Current.Server.MapPath( ". "));
------解决方案--------------------
<appSettings>
<add key= "connstr " value= "provider=microsoft.jet.4.0;data source= "> </add>
<add key= "dbpath " value= "drugbase.mdb ">
</appSetting>

用的时候
string connstr=System.Configuration.ConfigurationSettings.AppSettings[ "connstr "]+ System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings[ "dbpath "]);
------解决方案--------------------
System.Web.HttpContext.Current.Request.ServerVariables[ "APPL_PHYSICAL_PATH "];也能获取项目的路径
------解决方案--------------------
<add name= "connStr " connectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\data.mdb " providerName= "System.Data.OleDb "/>

------解决方案--------------------
skylarkzcc(抱膝独坐在风中) ( ) 信誉:100 Blog 加为好友 2007-05-09 09:37:23 得分: 0


<add name= "connStr " connectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\data.mdb " providerName= "System.Data.OleDb "/>



最佳答案!!
------解决方案--------------------