none
Where to save Access database in VB.net project?

    Question

  • I created a vb.net project with access database. I have saved database in bin directory and in the CONNECTION STRING gave address as "DATA SOURCE=Paper_name.mdb". By doing this, it is working. but when I create a setup file and install in other systems(In my system it works perfectly) which does not have Visual studio then I am getting connection error.

    Please tell me where should I place by database and what path should I give in the Connection string.

    Thank you

    Monday, March 13, 2017 5:51 PM

Answers

  • You may get a number of different answers, but one place I would not put it is the application folder or subfolder. I always put Access database in an application subfolder of the Documents folder:

    Dim MyDatabasePath = String.Concat(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "\AppName\MyDatabase.accdb")


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by Tabzee Wednesday, March 15, 2017 9:26 AM
    • Unmarked as answer by Tabzee Wednesday, March 15, 2017 9:26 AM
    • Marked as answer by Tabzee Friday, March 17, 2017 7:26 PM
    Tuesday, March 14, 2017 12:40 PM
  • I don't know which version of Visual Studio you are working with, but in your setup project you can put the database in a subfolder of the User's Personal Data folder (Documents library).

    A file installed in this location will be found under:

    C:\Users\<user name>\My Documents\<app folder>


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by Tabzee Friday, March 17, 2017 7:19 PM
    Thursday, March 16, 2017 12:01 PM

All replies

  • https://blogs.msdn.microsoft.com/patricka/2010/03/18/where-should-i-store-my-data-and-configuration-files-if-i-target-multiple-os-versions/

    Same thing happen to me before with some type of data file. Moving it to app data solved the editing problem.

    So my app data path ended up being: C:\Users\georg\AppData\Local


    • Edited by George-Frias Wednesday, March 15, 2017 7:10 PM turned hyperlink text into a hyperlink
    Monday, March 13, 2017 6:21 PM
  • Hi Tabzee,

    Welcome to MSDN forum.

    Our forum is to talk about the installation of visual studio, your issue is more related to visual basic, i will move your thread to Visual Studio Languages , .NET Framework > Visual Basic for a professional support.

    Best regards,

    Joyce


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 14, 2017 7:23 AM
  • Hi Tabzee,

    You could use Absolute path to the connection string. like this:

    string path = Directory.GetCurrentDirectory();
    
    string connstr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=@{0}\\myAccessFile.accdb;Persist Security Info = False; ", path);
    

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Tabzee Wednesday, March 15, 2017 9:45 AM
    • Unmarked as answer by Tabzee Wednesday, March 15, 2017 9:45 AM
    Tuesday, March 14, 2017 7:42 AM
    Moderator
  • You may get a number of different answers, but one place I would not put it is the application folder or subfolder. I always put Access database in an application subfolder of the Documents folder:

    Dim MyDatabasePath = String.Concat(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "\AppName\MyDatabase.accdb")


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by Tabzee Wednesday, March 15, 2017 9:26 AM
    • Unmarked as answer by Tabzee Wednesday, March 15, 2017 9:26 AM
    • Marked as answer by Tabzee Friday, March 17, 2017 7:26 PM
    Tuesday, March 14, 2017 12:40 PM
  • Thank you for your response.

    If I include my access database in Document folder and then specify the path in the project, then will it not become machine dependent? because in other systems  path with same folder names will not exist.

    I want to create a setup of the project and then install in other computers to run the project.Presently I have included my database in bin folder,and It runs perfectly in my system.But when I install in other systems I am getting connection error.  So for this issue where do you recommend me to include my Access database, so that it will not give connection error If I install in other systems.

    Waiting for your response.

    Thank you

    Wednesday, March 15, 2017 9:34 AM
  • Thank you for your response.

    I am doing a vb.net project, so can you please give me the above code in vb.net.

    Waiting for your response

    Thank you

    Wednesday, March 15, 2017 9:46 AM
  • Thank you for your response.

    If I include my access database in Document folder and then specify the path in the project, then will it not become machine dependent? because in other systems  path with same folder names will not exist.

    I want to create a setup of the project and then install in other computers to run the project.Presently I have included my database in bin folder,and It runs perfectly in my system.But when I install in other systems I am getting connection error.  So for this issue where do you recommend me to include my Access database, so that it will not give connection error If I install in other systems.

    Waiting for your response.

    Thank you

    The configuration is user specific. Unless a user completely removes their Documents special folder, which would be highly unusual because other apps use it as well, the database will be kept under an application subfolder at this location. You are not specifying a physical path to the file. The path to the file is known through the use of:

    System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

    The bin folder of your application would be under your application folder under Program Files or Program Files (x86). These folders are protected by Windows UAC virtualization so the actual location of the database, when updated, will be redirected to the user's profile folder. This can be rather confusing so I would not recommend using this approach.



    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, March 15, 2017 11:43 AM
  • Thank you for your response.

    Can you please give me the above code for vb.net

    Wednesday, March 15, 2017 7:00 PM
  • Thank you for your response. It is really informative.

    Can you please guide me regarding how to place the database file in MyDocuments folder during installation of my project.

    If I place my database in MyDocuments folder and use this following code System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) then It will execute in my system. But when I include it in my Setup file, and build it, and then install it in other system then I will get an connection error because database base will not be present there.

    So can you please tell how to bring the database under MyDocuments folder during installation?

    (I use VISUAL STUDIO INSTALLER to create setup file of my project, and I have place my database and Program output under FILE SYSTEM UNDER TARGET MACHINE->APPLICATION FOLDER )

    So do I need to specify anything during setup so that when user install the project , the database will be stored in MYDocuments

    And also do you know where the database will be stored in computer after installation?

    Waiting for your response.

    Thank you

    Wednesday, March 15, 2017 7:32 PM
  • Thank you for your response. It is really informative.

    Can you please guide me regarding how to place the database file in MyDocuments folder during installation of my project.

    If I place my database in MyDocuments folder and use this following code System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) then It will execute in my system. But when I include it in my Setup file, and build it, and then install it in other system then I will get an connection error because database base will not be present there.

    So can you please tell how to bring the database under MyDocuments folder during installation?

    (I use VISUAL STUDIO INSTALLER to create setup file of my project, and I have place my database and Program output under FILE SYSTEM UNDER TARGET MACHINE->APPLICATION FOLDER )

    So do I need to specify anything during setup so that when user install the project , the database will be stored in MYDocuments

    And also do you know where the database will be stored in computer after installation?

    Waiting for your response.

    Thank you

    Thursday, March 16, 2017 9:13 AM
  • I don't know which version of Visual Studio you are working with, but in your setup project you can put the database in a subfolder of the User's Personal Data folder (Documents library).

    A file installed in this location will be found under:

    C:\Users\<user name>\My Documents\<app folder>


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by Tabzee Friday, March 17, 2017 7:19 PM
    Thursday, March 16, 2017 12:01 PM
  • Thanks a lot sir.

    This solved my problem.

    Friday, March 17, 2017 7:19 PM