Jackery Xu
Jackery Xu

Reputation: 402

oledb can't connect

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.

Upvotes: 0

Views: 399

Answers (2)

rotgers
rotgers

Reputation: 2010

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.

Upvotes: 1

Jakub Januszkiewicz
Jakub Januszkiewicz

Reputation: 4418

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

Upvotes: 0

Related Questions