1

I'm building a small VB.net app. I want to add a little database (about 5 columns, 20 records). I want to keep everything in a single exe. I think it's a bit overkill to add a 'full' database, so I'm looking for an alternative. I could create a CSV file, and add it as a resource. Is this a good idea, or are there any other better alternatives?

1
  • 2
    Data is Data. By its nature it can change. Embedding anything means that if it changes you have to recompile and redistribute the app. If the data is in an XML you can load it to a datatable, and send replacements if/when needed. Commented Jul 23, 2016 at 17:20

2 Answers 2

1

Another option for such a small amount of data is to store it in ApplicationSettings. Your question implies you are using WinForms, so you can make use of the built-in features with just a small amount of work to store your own custom class.

  1. Create a class to represent your data.
  2. Wrap that class in a property of another class that inherits from ApplicationSettingsBase as a List(Of )
  3. Manipulate this custom setting as needed and call Save() as needed.

Here is an example that binds to a DataGrid:

The class that represents your data:

Public Class Fruit
    Public Property FruitName As String
    Public Property FruitColor As String
    Public Property FruitGrowsOn As String
End Class

The class that turns Fruit into a collection stored in application settings. Notice it inherits ApplicationSettingsBase. Also notice the attributes on the Fruits property that identify this as a user setting as opposed to an application setting (which cannot be modified by the user). The DefaultSettingAttribute makes sure the collection is instantiated so you don't get null reference exception until after the first time you add an item:

Imports System.Configuration

Public NotInheritable Class FruitCollection
    Inherits ApplicationSettingsBase

    <UserScopedSettingAttribute()>
    <DefaultSettingValue("")>
    Public Property Fruits() As List(Of Fruit)
        Get
            Fruits = Me("Fruits")
        End Get
        Set(ByVal value As List(Of Fruit))
            Me("Fruits") = value
        End Set
    End Property
End Class

The Form definition. Retrieves the instance of your custom setting (FruitUserSettings), creates a binding source for a DataGridView, and provides a Save button to persist the changes made in the grid to Settings. Next time the user opens the form the changes will still be there provided they clicked the Save button:

Public Class Form1

    Dim FruitUserSettings As FruitCollection
    Dim GridBindingSrc As BindingSource

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        FruitUserSettings = New FruitCollection()

        GridBindingSrc = New BindingSource(FruitUserSettings, "Fruits")

        DataGridView1.AutoGenerateColumns = True
        DataGridView1.DataSource = GridBindingSrc

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        FruitUserSettings.Save()
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        GridBindingSrc.Dispose()
    End Sub
End Class

Note, you don't need a binding source or grid, these were just for demonstration. You can manipulate FruitUserSettings.Fruits like any other list in any way you want. As long as Save() is called on the settings you will retain the data.

You can download/clone the working sample here: https://github.com/crowcoder/CustomSetting

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

i would use XML file as little database, you can query it easily with linq (Language-Integrated Query). also there are built in library's that can help you handle you records and query's. of course that you can use access, excel (you can query excel with SQL) csv or txt file . also you can create a local data base file in visual studio

2 Comments

or sql ce database
...or a binary serialized file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.