Link to home
Start Free TrialLog in
Avatar of bengelhart
bengelhart

asked on

Preload Data Into VB App

Is it possible to preload data into a vb.net app?  I don't want to send a db with it either.  I want it to be completely transparent ot the client, so that they only see the executable and that is it.  This app can't be installed on the client's harddrive - it must run from a disk.
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

You could put data into an assembly (i.e., a separate file from the code) that gets built into the executable. You'd probably want to make it a class module for easy reference. Do you have a lot of data to load? Is having it all in text format in the class module file OK, or do you have pictures and other data for which text doesn't work well? Is this data that will be different for each client, or the same for everyone whom you send the program to?
Avatar of bengelhart
bengelhart

ASKER

It will all be text values, and yes it will be different for each client.  The record count will be anywhere from 500 to 8000.
Presumably the records are actually coming from a database that you have. There may be a way to store the db inside your executable, and then load the data when the program starts, but that requires a level of expertise above mine. 8-) Another option would be to create a loading program (that's used only on your computer) that reads your database and creates a text file containing the data structured as a class module. The output file would then be built into your executable.

So the output would look something like:

Public Class StandardData

   Public strCountries(50) as String
   strCountries(0) = "United States"
   ...

End Class

You'd then have a reference in your program to myapp.StandardData.strCountries(0) for "United States" (where "myapp" is the name of the root namespace for your application). You can put "Imports myapp.StandardData" at the beginning of your regular program file to avoid having to reference "myapp" each time.
Cool I will try that.  Thanks.  That should definitely do the trick.  I am going to try it out tomorrow.  I will respond to this question and accept the answer if it works.  IF anyone knows how to attach a db lick ElrondCT is saying please feel free to jump in!
Correction: I should have verified my suggestion before making it. It needs to be rewritten as follows:

Public Class StandardData

   Public Shared strCountry() = { _
      "United States", _
      "Canada", _
      ...
      "Zimbabwe" }

End Class

then in your application, you can use myapp.StandardData.strCountry(1) (or if you specify "Imports myapp.StandardData" at the beginning of your form code, you can just use strCountry(1) ).

You can, of course, have as many items on a line as you want, but since you'll be generating this from a database, it'll probably be easiest to have each on its own line, with the _ continuation character to tie everything together. One annoyance is that VB doesn't allow comments in the middle of a logical statement, so there's no way to notate in the code what subscript each element has.

My apologies for not checking this out more carefully originally, but the above is tested and I think will do what you want.
Excellent thanks.  I have about 8 columns per record, so I will just create a class for each column so that the index for each column is equal.  How would I create a loading function for something like this?  Does VB.NET support creating classes through functions?
What I have in mind is a second application, used only to create the class module. It would use a StreamWriter to output the data you want in the class. All of the columns could be in a single class. So it would look something like this:

        Dim swriOut As New System.IO.StreamWriter("StructuredData.vb")
        Dim strOutCountry(5) As String
        Dim strOutCity(6) As String
        Dim i As Integer

        swriOut.WriteLine("Public Class StrcturedData")
        swriOut.WriteLine("")
        swriOut.WriteLine("Public Shared strCountry() as String = { _")
        For i = 0 To strOutCountry.GetUpperBound(0) - 1
            swriOut.WriteLine("""" & strOutCountry(i) & """, _ ")
        Next
        swriOut.WriteLine("""" & strOutCountry(strOutCountry.GetUpperBound(0)) & """}")

        swriOut.WriteLine("Public Shared strCity() as String = { _")
        For i = 0 To strOutCity.GetUpperBound(0) - 1
            swriOut.WriteLine("""" & strOutCity(i) & """, _ ")
        Next
        swriOut.WriteLine("""" & strOutCity(strOutCity.GetUpperBound(0)) & """}")

        swriOut.WriteLine("End Class")
        swriOut.Close()

Once you've run this application, you add StructuredData.vb to your real application (right-click on the project in the Solution Explorer, choose Add, then Add Existing Item). It's now ready for the program to use. When you want to recompile the application for a different client, you run the StreamWriter application again and pop the new file into the primary application. Does that make sense?
Yeah I think that will work.  I will test it tomorrow to see how it goes.  I will get back to you.  Thanks!!!!
Actually I would like to use an XML file as an embedded resource to do this.  I just can't figure out how to read it.  here is my code so far:

        Dim ExecAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
        Dim stName As String = ExecAssembly.GetName.ToString
        Dim Str As IO.Stream = ExecAssembly.GetManifestResourceStream(stName & ".ASQ.xml")
        ds.ReadXml(Str)

        'dgList is a datagrid
        dgList.DataSource = ds
        dgList.Refresh()
You're in an area of the language that I have no experience with, so I can't offer any help. Sorry. You might want to post a new question specifically asking how to read an XML file that's an embedded resource.

Another thread that I found that might be useful for you is https://www.experts-exchange.com/questions/21259587/Best-way-to-save-user-info-settings.html.  It shows how to save variables to an XML file and read them back. It presupposes an XML file that is external to the application, but it may be possible to use the basic ideas and tweak it to work with an embedded file.
Got it figured out now.  My namespace had spaces in the name so those spaces needed to be underscores.  
ASKER CERTIFIED SOLUTION
Avatar of GranMod
GranMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial