Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Lounger
    Join Date
    May 2003
    Posts
    29
    Thanks
    3
    Thanked 1 Time in 1 Post

    VB.Net and databases

    I've managed to convert a program I wrote some time ago in VB6 to VB.net - now, it's probably not as efficient as it could be, but I have got it to work which I think is a good starting point. I can therefore get it to run for ME with no problem, but it's a program I hope to share with others - I've used an Access database as the source, and what I'm struggling to find (despite lots of searching) is how to tell my application WHERE to find the database on a machine where the user could have installed any drive for a location! My app currently uses a dataset assigned at design time. Any clues as to how I do this would be great. My old method (VB6) was to use
    Data1.DatabaseName = App.Path & "\MyDatabase.mdb"
    to set the location

  2. #2
    jwoods
    Guest
    This is a good resource...

    https://www.connectionstrings.com/access/

  3. #3
    Administrator
    Join Date
    Jun 2010
    Location
    Portugal
    Posts
    12,519
    Thanks
    152
    Thanked 1,398 Times in 1,221 Posts
    Probably you should use a app.config file and store the connection string there, read it at runtime and connect to the database.This means that all you'd need to do would be to edit a text file to specify a new connection string for your app to use whenever it runs.
    Rui
    -------
    R4

  4. #4
    Lounger
    Join Date
    May 2003
    Posts
    29
    Thanks
    3
    Thanked 1 Time in 1 Post
    it's a nice page certainly - sadly, I cannot see how that addresses my question? My app is currently in VB 2010 , though I have 2015 installing at the moment and will no doubt have to convert again!

  5. #5
    Lounger
    Join Date
    May 2003
    Posts
    29
    Thanks
    3
    Thanked 1 Time in 1 Post
    thanks - that makes sense Ruirib - is there a good resource for using the app.config file?

  6. #6
    WS Lounge VIP Browni's Avatar
    Join Date
    Dec 2009
    Location
    Rochdale, UK
    Posts
    1,651
    Thanks
    38
    Thanked 161 Times in 139 Posts
    Why not just use a dialogue window to ask the user where it is?

  7. #7
    jwoods
    Guest
    Quote Originally Posted by aidan100 View Post
    it's a nice page certainly - sadly, I cannot see how that addresses my question? My app is currently in VB 2010 , though I have 2015 installing at the moment and will no doubt have to convert again!
    Doing a little more research, you'll find that Application.StartupPath is an equivalent using Windows Forms.

    If your database won't be installed in the same location on every system, neither this or app.config will help.
    Last edited by jwoods; 2015-11-11 at 17:21.

  8. #8
    Lounger
    Join Date
    May 2003
    Posts
    29
    Thanks
    3
    Thanked 1 Time in 1 Post
    thanks Browni - finding where it is is one thing, how do I make my app start to use it - when I designed it, a wizard (how I hate those) helpfully built my connection- to the location on my dev machine. so I guess the location is hard coded - what do I need to do to get my app to use any other location? It's that part that is causing me the problem at the moment!!

  9. #9
    Lounger
    Join Date
    May 2003
    Posts
    29
    Thanks
    3
    Thanked 1 Time in 1 Post
    yes, I've used application.startuppath within my application - it also interfaces with a word template, so that bit used that - what I'm struggling with is how to tell my app which database to use.

  10. #10
    jwoods
    Guest
    You would need to do a recursive search of all directories...

    https://support.microsoft.com/en-us/kb/306666

  11. #11
    Lounger
    Join Date
    May 2003
    Posts
    29
    Thanks
    3
    Thanked 1 Time in 1 Post
    I really wouldn't! Let's assume (as it's true) that I can know after installation where the database is - lets say (to keep it simple) it's in the location of the app. so we can use app.startuppath or similar to get that. Lovely. BUT how do I use this with my database app. My data source is (I assume) hard coded to the location of my dev database. At least, that's how it appears to me. I'm probably wrong, but hard to be sure!

  12. #12
    Lounger
    Join Date
    May 2003
    Posts
    29
    Thanks
    3
    Thanked 1 Time in 1 Post
    I can see that my original question is written in words that made sense to me at the time, but probably don't help. For which my apologies. Thanks for all the help about finding a file - I think we've got that covered really well. It's the "how to actually use that location" bit that I'm having issues with.

  13. #13
    jwoods
    Guest
    I think you would benefit taking some time to do a few tutorials on VB.NET and database access.

    In the Visual Studio debugger, display the contents of App.StartupPath.

    If the DB resides there, and will always reside there, it's pretty simple...use that path, and an entry in app.config for the file name to create the Connection String.

    If you have no idea where it's at, then you will need to recursively search to get the path to build the Connection String.

    Hard-coding a location is never a good idea in a production application.

  14. #14
    Administrator
    Join Date
    Jun 2010
    Location
    Portugal
    Posts
    12,519
    Thanks
    152
    Thanked 1,398 Times in 1,221 Posts
    Quote Originally Posted by aidan100 View Post
    thanks - that makes sense Ruirib - is there a good resource for using the app.config file?
    Aidan, try this: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    Rui
    -------
    R4

  15. #15
    Lounger
    Join Date
    May 2003
    Posts
    29
    Thanks
    3
    Thanked 1 Time in 1 Post
    Quote Originally Posted by jwoods View Post
    I think you would benefit taking some time to do a few tutorials on VB.NET and database access.

    In the Visual Studio debugger, display the contents of App.StartupPath.

    If the DB resides there, and will always reside there, it's pretty simple...use that path, and an entry in app.config for the file name to create the Connection String.

    If you have no idea where it's at, then you will need to recursively search to get the path to build the Connection String.

    Hard-coding a location is never a good idea in a production application.
    Agreed - I'd love to do some tutorials on VB.NET - at the moment, my tutorial has been a lot of googling, which has found some useful and some LESS than useful resources - microsoft often coming into the second category - any pointers to good tutorials (or good google search terms) would be much appreciated. As you can see from my previous VB6 example, I agree that a hard coded location is bad, but (and I'm sure I'm wrong) the design method seems to almost force you to a hard coded location. I guess a good tutorial would help clarify that!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •