File Location For Access Databases
File Location For Access Databases
File Location For Access Databases
File Location For Access Databases
File Location For Access Databases
File Location For Access Databases File Location For Access Databases File Location For Access Databases File Location For Access Databases File Location For Access Databases File Location For Access Databases File Location For Access Databases File Location For Access Databases
File Location For Access Databases File Location For Access Databases
File Location For Access Databases
Go Back  Xtreme Visual Basic Talk > > > File Location For Access Databases


Reply
 
Thread Tools Display Modes
  #1  
Old 03-19-2010, 12:28 PM
sirmilt sirmilt is offline
Contributor
 
Join Date: Jan 2003
Location: Florida
Posts: 451
Default File Location For Access Databases


I have been using Visual for quite a while but have never published a programto be used on machines that I have no control over.

I'm using Visual Basic 2008 Professional and Windows 7 Professional.

I have been under the illusion that the best place to file the Access database is in the root directory, C: so that I can be sure that the connection in my Visual Basic code is always available.

With Windows 7, I am not sure where to file the database. Is there a preferred location?

Thank You
Reply With Quote
  #2  
Old 03-19-2010, 01:45 PM
AtmaWeapon's Avatar
AtmaWeaponFile Location For Access Databases AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Application data goes in a folder referred to using environment variable syntax: %APPDATA%.

The weird syntax is used because the location varies between pre- and post-Vista versions. It also varies by application. In .NET, you can get the path to the root of the application data folder with the Environment.GetFolderPath() method. It's customary to create a folder within this folder to store your application data. Microsoft recommends "\<Company Name>\<Application Name>\<Version>" but when I do private projects I don't have a company name and I like to keep all my files version-agnostic so I use just the application name.

For example, here's a .NET method that returns an appropriate path for "FooCorp Point of Sale Deluxe". The best practice is to look at the assembly information to get the company name and application name, but I'm usually too lazy and just hardcode it because I rarely change the application name. I usually put a Shared method like this one in some global class with a generic name like "Paths".
Code:
Function GetAppDataPath() As String
    Dim appDataRoot As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
    Dim companyName As String = "FooCorp"
    Dim applicationName As String = "Point of Sale Deluxe"

    Dim fullPath As String = IO.Path.Combine(appDataRoot, companyName)
    fullPath = IO.Path.Combine(fullPath, applicationName)

    Return fullPath
End Function
Now, you asked about this in an installer context, so maybe you can't use .NET code to specify this directory. No problem. In your setup project, display the file system editor (View>Editor>File System). Select the "File System on Target Machine" folder and right-click it. Choose "Add Special Folder>User's Application Data Folder". In that folder, add the folders needed for your application. Continuing the example above, just add "FooCorp" then add "Point of Sale Deluxe" inside that folder. Put the database in there.

In code, you can use the method above to figure out where your database is. In fact, you can make your life a little easier and piggyback off of GetAppDataPath():
Code:
Function GetDatabasePath() As String
    Dim appDataPath As String = GetAppDataPath()
    Dim databaseFileName As String = "records.db"

    Return IO.Path.Combine(appDataPath, databaseFileName)
End Function
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 03-19-2010, 06:06 PM
sirmilt sirmilt is offline
Contributor
 
Join Date: Jan 2003
Location: Florida
Posts: 451
Default

Thank You for the help. You poited out beautifully.

My only question is how would I show this in the Connection String?
My guess would be this:
"Provider=Microsoft.Jet.OLEDB.4.0; "Data Source = C:\appPath\MyDatabase.mdb;"

Milt
Reply With Quote
  #4  
Old 03-22-2010, 08:35 AM
AtmaWeapon's Avatar
AtmaWeaponFile Location For Access Databases AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Build the string programmatically.

Towards the end of my post, I demonstrated the GetDatabasePath method, that answers the question, "Gee, I know how to get the path to the AppData folder on this system, but I want to put 'MyDatabase.mdb' on the end of it."

Now your problem is, "Gee, I know how to get the full path to the database file, but I want to put it inside the connection string." Write a function that uses String.Format() to put it in there.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 03-25-2010, 09:43 AM
sirmilt sirmilt is offline
Contributor
 
Join Date: Jan 2003
Location: Florida
Posts: 451
Default

Thanks for the help, it works fine.

Milt
Reply With Quote
Reply

Tags
database location


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
File Location For Access Databases
File Location For Access Databases
File Location For Access Databases File Location For Access Databases
File Location For Access Databases
File Location For Access Databases
File Location For Access Databases File Location For Access Databases File Location For Access Databases File Location For Access Databases File Location For Access Databases File Location For Access Databases File Location For Access Databases
File Location For Access Databases
File Location For Access Databases
 
File Location For Access Databases
File Location For Access Databases
 
-->