Link to home
Start Free TrialLog in
Avatar of troyhalsey
troyhalsey

asked on

Storing Data

So I am new to VB.Net...learned VB from VBA for Excel.  I have a big book of how to's but I am hoping I can get a simple answer.  

Is the only way to create large lists of information (i.e database or spreadsheet)....by creating an sql database?  I want my list to be stored within the program itself...a stand alone file not tied to any server database.  So how do I store my information to pull from like in Excel or Access?  Is there not a grid tool VB.net?  Sorry for the simple question...starting from scratch here.  Thanks to everyone in advance for your patience.

Troy
Avatar of marchiano
marchiano

Excel file itself can be a database to store information.
VB.Net is capable to open an excel file, read information and then show it to the user.
someone has posted a question on how to read and open excel file, .e.g
https://www.experts-exchange.com/questions/20972338/reading-open-Excel-file.html

if you want to access, it will be better.
but you will need to migrate all of your data from excel to access
Avatar of troyhalsey

ASKER

What if I don't want to reference another program? Is there no way to store data within the application itself?  My concern is this application I am creating will be used on and off of a network.  So I don't want to have the application tied to any other outside source.  
You should investigate Serialisation (serialization).

You can create a class for your data and use serialisation to save it to a file as binary or XML.  Have a look at this little example ... it is a class that can save/load all its property values in a binary data file.

'-----------------------------------------------------------------------------------------
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.io

<Serializable()> Public Class DataStorage

    Public DataItem as Integer
    Public DataItem2 as String
    Public DataItem3(5) as Boolean

    Public Sub Load(ByVal filepathname as string, ByRef ds as DataStorage)
        Dim formatter As New BinaryFormatter
        Dim fs As FileStream
        Try
            fs = New FileStream(filepathname, FileMode.Open)
            ds = CType(formatter.Deserialize(fs), DataStorage)
            fs.Close()
        Catch ex As Exception
            MsgBox("Unable to load from file [" & filepathname & "]" & vbcrlf & ex.message)
        End Try
    End Sub

    Public Sub Save(ByVal filepathname as string)
        Dim formatter as New BinaryFormatter
        Dim fs as FileStream
        Try
            fs = New FileStream(filepathname, FileMode.Create)
            formatter.Serialize(fs, Me)
            fs.Close()
        Catch ex as Exception
            MsgBox("Unable to save to file [" & filepathname & "]" & vbcrlf & ex.message)
        End Try
    End Function
End Class
'-----------------------------------------------------------------------------------------

To use something like this .....

Dim ds as New DataStorage
ds.DataItem = 42
ds.DataItem2 = "Hello World
ds.DataItem3(2) = True
ds.DataItem3(4) = True
ds.Save("C:\DataStorage.dat")
ds = nothing

Dim msg as string
Dim i as integer
ds = new DataStorage
ds.Load(ds)
msg = ds.DataItem & vbcrlf & ds.DataItem2 & vbcrlf
For i=1 to 5
    msg += DataItem3(i) + " "
Next
MsgBox(msg)
ASKER CERTIFIED SOLUTION
Avatar of jrscherer
jrscherer
Flag of United States of America image

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
Jack-

So if I use a DataSet...when I package the application.  It would all go with the application...no loose ends?

Troy