在类中如何用相对路径设置连接字符串?

CodeVitamin 2006-10-25 06:11:42
我写了一个Access数据库操作的类,让页面调用里面的方法。
现在的问题是 怎么用相对路径设置连接字符串?
因为类里面不能用Server内置对象,所以只能用AppDomain,但是用他以后在本地测试没有问题,但是传到服务器浏览就出现问题。
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".


<!-- Web.Config Configuration File -->

<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>


Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.


<!-- Web.Config Configuration File -->

<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>

不知道还有没有什么在类中设置相对路径连接字符串的方法?
...全文
235 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ispovo 2006-11-17
  • 打赏
  • 举报
回复
用net_lover(【孟子E章】) 这个就没问题了。
因为在服务器上你不知道你的文件在具体哪一个文件夹,用HttpContext.Current.Server.MapPath("~")可以获取当前的目录,再加上其他的参数就搞定了。
孟子E章 2006-11-17
  • 打赏
  • 举报
回复
<appSettings>
<add key="con" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="></add>
<add key="data" value="/Data/mdb"></add>
</appSettings>

string myCon=System.Configuration.ConfigurationSettings.AppSettings["con"]+ HttpContext.Current.Server.MapPath("~" + System.Configuration.ConfigurationSettings.AppSettings["data"]);
sxlfybb 2006-11-17
  • 打赏
  • 举报
回复
使用"~"来判断文件相对路径
~表示应用程序根目录.

就尽量避免使用../来判断文件路径.

在win2003里,默认是不启用父路径的.
redebug 2006-11-17
  • 打赏
  • 举报
回复
<appSettings>
<add key="con" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="></add>
<add key="data" value="../**/***.mdb"></add>
</appSettings>

页面中的连接字符串这样写:
private static string myCon=System.Configuration.ConfigurationSettings.AppSettings["con"]+ HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["data"]);

private OleDbConnection con=new OleDbConnection(myCon);

//private SqlConnection con=new SqlConnection(myCon);

62,133

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧