1

I have a project using an Access database. I go to data connections in VS and create a connection, I select the db which has a path of [my project folder]\app_data\stats.accdb and test the connection and it works fine, so I save it. VS then doesn't allow me to access the DB, it puts a red X on it under data connections in server explorer. When I go to modify the connection, the path has been changed to |DataDirectory|\stats.accdb.

This would be fine, but when I test the connection, it says Could not find the file 'c\program files\Microsoft visual studio\2022\Community\Common7\IDE\stats.accdb'

How to I change it so the |Data Directory| points to where the database is?

1 Answer 1

0

|DataDirectory| is a substitution string so you can configure the location of your database file separately.

For example:

// Set |DataDirectory| value

AppDomain.CurrentDomain.SetData("DataDirectory", "C:\xxx");

// Connection String with |DataDirectory| substitution string

String STR = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Student.accdb"; OleDbConnection connection = new OleDbConnection(STR);

For more information about |DataDirectory|, you can refer to this post: https://stackoverflow.com/a/1409378/11507778

2
  • But I haven't set it anywhere in code, so where is it pulling the path from? It's getting it from somewhere in the IDE, but where? It's not a runtime issue. I can't browse the connection in the IDE because it's saying |DataDirectory| is set to something that I never set. I don't use |DataDirectory| in my code at all, I just reference the connection I created.
    – DennisB
    Commented Jul 9 at 18:11
  • Regarding |DataDirectory|, this may be some specific settings in VS. In addition, everything is normal after my test. Your situation may be caused by some of your operations or settings. If possible, you can try to create a new project to see if you can reproduce this problem.
    – Cody Liang
    Commented Jul 10 at 3:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.