0

Want to connect to a .mdb file in the App_Data folder.

Connection string:

"Provider=Microsoft.JET.OLEDB.4.0;data source=|App_Data|\\abcd.mdb"

Receive error:

Not a valid file name.

When adapter tries to fill the dataset.

Tried foreslash, backslash, squiggly line, you name it. Nothing seems to work.

Please help, thanks.

Jackery Xu
  • 386
  • 2
  • 5
  • 19
  • 1
    Well I am afraid `Jackery Xu` you will need to show more code than that.. also your connection string looks a bit off – MethodMan Apr 02 '13 at 19:24
  • http://www.connectionstrings.com/access what about just one backslash ? – adt Apr 02 '13 at 19:27

2 Answers2

1

I know this is a little too late but I had the same issue as you questioned here and this is what worked for me:

string con = String.Format(
        "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=True;", 
         String.Format(
                @"{0}\{1}",
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                "abcd.accdb"
         )
);

The @ enables you to use characters like \ without backslashing these. I always use @ when entering filepaths. Although I can imagine that hard-coding filepaths in your code is probably not the best way to do it, try this instead (What is app.config for?).

The line Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) resolves to the value of the environment variable %APPDATA%.

Also note that this solution is for accdb files and if you want to use this for mdb you need to change the Provider to Microsoft.JET.OLEDB.4.0.

I hope this answer helps someone.

Community
  • 1
  • 1
rotgers
  • 1,992
  • 1
  • 15
  • 25
0

I think it you should use |DataDirectory|, not |App_Data|. And a single backslash after it.

Jakub Januszkiewicz
  • 4,380
  • 2
  • 37
  • 54