在 C# 中为 Access DB 使用连接字符串中的相对路径 [英] Using a relative path in connection string for Access DB in C#

查看:22
本文介绍了在 C# 中为 Access DB 使用连接字符串中的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 web.config 文件中的这一行使用相对路径而不是硬编码的路径,但似乎没有任何效果.我只能找到 SQL 和 mySQL DB 的东西

I'm trying to get this line in my web.config file to use a relative path instead of hardcoded one, but nothing seems to be working. I can only find stuff for SQL and mySQL DBs

<connectionStrings>
<add name="dbConnection" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersMikeDesktopGeauxEat NEWGeauxEatApp_DataGeauxEatAccessDB.accdb"/>
</connectionStrings>

我试过了

<add name="dbConnection" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|Data Directory|GeauxEatAccessDB.accdb"/>

但随后它会在此文件夹中查找不存在的内容.

but then it looks for something in this folder where it doesn't exist.

"C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0"

有什么办法让它相对吗?它位于项目文件的AppData文件夹中

Is there any way to get it relative? It's located in the AppData folder of the project file

推荐答案

我环顾四周,让它工作起来.我将此行插入到 Global.asax 文件的 Application_Start 方法中

I looked around and got it working. I inserted this line into the Application_Start method of the Global.asax file

AppDomain.CurrentDomain.SetData("DataDirectory", Server.MapPath("~/App_Data/"));

这篇关于在 C# 中为 Access DB 使用连接字符串中的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆