none
Add Database connect: Relative path RRS feed

  • Question

  • Hello,

    I am having some trouble adding an Access database to Visual studio. Instead of copying the database from "local storage", I would like to have it look immediately towards \bin\Debug\dbUsers.accdb.

    This software must be run on another system and therefore having an absolute path to the file C:\Users\Default\Desktop\etc will not work. 

    The database I would like it to access is located in the project Debug/output folder (\bin\Debug\dbUsers.accdb). 

    The project folder is VicRoads Logging Application\bin\Debug

    As seen above, I get a test connection successful, however when I hit Okay and Next, I get this error:

    Please note that I should only have 3 "VicRoads Logging Application" Folders.

    Does anyone know why the first connection start looking in a different folder to the second test, and how to fix this?

    Cheers!

    Wednesday, August 16, 2017 12:28 PM

All replies

  • The default behavior for working with data this way is to place the database in the same folder as the executable. When done this way the app.config will have a connection string as follows which at runtime resolves to the current folder.

    Suppose I want the data to come from C:\Data (or another existing folder), in form load I can use

    Public Class Form1
        Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
            Handles CustomersBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.CustomersBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DataSet1)
    
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            If IO.Directory.Exists("C:\Data") Then
                If IO.File.Exists("C:\Data\NorthWind_1.accdb") Then
                    AppDomain.CurrentDomain.SetData("DataDirectory", "C:\Data")
    
                    ' TODO: This line of code loads data into the 'DataSet1.Customers' 
                    ' table. You can move, or remove it, as needed.
                    Me.CustomersTableAdapter.Fill(Me.DataSet1.Customers)
                Else
                    ' database file does not exists
                End If
            Else
                ' path does not exist
            End If
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, August 16, 2017 12:43 PM
    Moderator
  • Hi Azuraxe,

    Yes, You can use | DataDirectory | to solve this problem.But you can also provide full path in connectionstring and try to connect Access DataBase.

    Like this:

      Dim conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Database\InsertDate.accdb; Persist Security Info=False;")

    Best Regards,

    Cherry


    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.

    Thursday, August 17, 2017 6:26 AM
    Moderator
  • Thank for your replies!

    I am now having trouble with adding the TableAdapter to the form. I get the errors below as I am unable to add a TableAdapter to the form via toolbox.

    Is there a way to create the TableAdapterManager and tblUserTableAdapter so that my program will run properly? As shown below, I have added the data source!

    Cheers

    Friday, August 18, 2017 11:22 AM
  • The image you are showing has no baring on the TableAdapterManager as the .xsd file is a representation of your data and the generated strong type classes while the TableAdapterManager manages those classes on a form, not the .xsd.

    A valid reason for the current issue is that the TableAdapterManager (and the BindingNavagator are missing from the form or somehow where renamed).

    Looking at the screenshot I noticed that the save click event for the BindingNavigator has no handles clause which (unless you did it) happens by the IDE when a control is removed. So my guess is the Table Adapter Manager is also not present.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, August 18, 2017 11:33 AM
    Moderator
  • Hi,
    You are exactly right. Sorry, I don't think I was clear enough!!!

    There is no TableAdapter or anything generating when I drag the DataSet onto the form, nor can I add it from the toolbox?

    Error 9 'TableAdapterManager' is not a member of 'VicRoads_Logging_Application.Register'

    Cheers


    • Edited by Azuraxe Sunday, August 20, 2017 11:07 AM
    Sunday, August 20, 2017 6:26 AM
  • Hi Azuraxe,

    Based on your description, I think there is a video that may be helpful to you, please refer to.

    https://www.youtube.com/watch?v=dBrAvAkCZR4

    I have tested it, the TableAdapterManager and TableAdapter are all display in the form.

    Best Regards,

    Cherry


    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.


    Thursday, August 24, 2017 8:45 AM
    Moderator