Using a relative path in connection string for Access DB in C#

▼魔方 西西 提交于 2019-12-17 17:01:58

问题


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:\Users\Mike\Desktop\GeauxEat NEW\GeauxEat\App_Data\GeauxEatAccessDB.accdb"/>
</connectionStrings>

I tried making it

<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"

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


回答1:


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/"));


来源:https://stackoverflow.com/questions/13169526/using-a-relative-path-in-connection-string-for-access-db-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!