Results 1 to 5 of 5

Thread: How can I choose by program to load from DB or xml-file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    How can I choose by program to load from DB or xml-file

    I work with MS Access database which works fine. Now, for a presentation I would like to run the .exe file on my tablet which works, too.
    Unfortunately, I do not have MS access there and would like to export and import the date using dataset.writexml and dataset.readxml. Even that seems to work as long as the tables have never been set up to load from DB.

    Now, I am not sure, how to stop the program from trying to load from the DB and rather load the xml data, instead, or even better, if the program would ask at start where to load from.

    Could somebody help, please?

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: How can I choose by program to load from DB or xml-file

    It takes some work and thought. What you need is "an abstraction layer".

    That means making an abstract type or interface that does all the things you want the DB or the text file to do. Then you make one implementation that uses a text file, and a different implementation that uses the real DB.

    Then, in your program, have some switch somewhere (or maybe a special build) that makes it choose the text file version.

    (An example would be sort of involved, and someone else might have to write it. I can't think of a way to write it that'd make you happy and not take me a few hours. I don't have Access, I don't use DataAdapters/DataSets, so I'd be fumbling along pretty slowly or writing stuff completely different from what you want to see.)
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Re: How can I choose by program to load from DB or xml-file

    from what say and thinking a little bit about it, it sounds like the problem are the tableadapters, which communicate conviently with the db. If they could be stopped or replaced by something else which loads the tables, then I could choose where the data should come from. Is this assumption corrrect?

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: How can I choose by program to load from DB or xml-file

    More or less, yes.

    At a high level, all your app probably cares about is getting a DataSet or some DataTables that you throw at a DataGridView or whatever. So conceptually, all your application needs is something like:
    Code:
    Public Interface IDataFetcher
    
        Function GetData() As DataSet
    
    End Interface
    The code you have right now more or less implements that using the TableAdapters:
    Code:
    Public Class DatabaseDataFetcher
        Implements IDataFetcher
    
        Public Function GetData() As DataSet Implements IDataFetcher.GetData
            ' Do things with TableAdapters
            Return results
        End Function
    End Class
    You could also make an implementation that uses files:
    Code:
    Public Class FileDataFetcher
        Implements IDataFetcher
    
        Public Function GetData() As DataSet Implements IDataFetcher.GetData
            ' Do things with files
            Return results
        End Function
    End Class
    The way you "stop or replace that" is to make a decision at the point where you load the data. Right now it probably looks like this:
    Code:
    Sub Button384_Click(...) Handles Button2384.Click
        ' Do things with TableAdapters
        DataGridView83.DataSource = results
    End Sub
    You'd change it to something like this:
    Code:
    Dim dataFetcher As IDataFetcher
    If My.Settings.IsUsingDatabase Then
        dataFetcher = New DatabaseDataFetcher()
    Else
        dataFetcher = New FileDataFetcher()
    End If
    
    DataGridView83.DataSource = dataFetcher.GetData()
    The alternative is to just hard-code the file code in that spot. It's a mess and I promise you'll find your life is improved if you use a "classes that implement an interface" approach for this problem every time you solve it.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2016
    Posts
    255

    Re: How can I choose by program to load from DB or xml-file

    thanks for input. I guess i will get it to work.

Posting Permissions

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



Click Here to Expand Forum to Full Width