Home > front end >  Which is the best way to store data from small static tables so I can easily access the data in C# W
Which is the best way to store data from small static tables so I can easily access the data in C# W

Time:10-17

I'm working in a Windows Form project using C# that will be used to make calculations.

Basically the objective of this project is to replace a big Excel spreadsheet I've been using. As more people will use it, I need to do it FoolProof.

An example of what I have to do: A comboBox should show me the column names (So I should retrieve the column names into a IEnumerable list). Then I should insert a value in a TextBox that corresponds to a value in the first column. The program will use thise and the column name into this table and save the corresponding value in memory

Table

Tables I'm going to use are mostly from books so I might never (or not often) change these values. That's why I dismissed using a SQL database. I thought it was like buying a Ferrari for going to the mall.

Thanks so much in advance

CodePudding user response:

The simplest storage for such data is a text file:

6;80;125

8;107;166

...

To store data write them line by line delimited by ';'.

To read data, read the file content line by line (or ReadAllText) then split each line by ';'.

CodePudding user response:

I agree with your point that an SQL database is probably an overkill for your use case. Here’s a relatively easy workaround.

In Excel, press File, Save as, Text (tab delimited).

Move that file into your C# project, add to the project, then right click in visual studio, Properties, set Build Action = Embedded Resource.

Then write some C# to parse data from the tab-delimited file. Here’s an example.

sealed class Entry
{
    public readonly double diameter, al, adn;

    Entry( string line )
    {
        string[] fields = line.Split( '\t' );
        if( fields.Length != 3 )
            throw new ArgumentException();
        diameter = double.Parse( fields[ 0 ], CultureInfo.InvariantCulture );
        al = double.Parse( fields[ 1 ], CultureInfo.InvariantCulture );
        adn = double.Parse( fields[ 2 ], CultureInfo.InvariantCulture );
    }

    public static Entry[] loadTabSeparated( Stream stream )
    {
        using var reader = new StreamReader( stream );
        // Ignore the first row with column names
        reader.ReadLine();
        // Parse the rest of the rows in the table
        List<Entry> list = new List<Entry>();
        while( true )
        {
            string? line = reader.ReadLine();
            if( null == line )
                break;
            list.Add( new Entry( line ) );
        }
        return list.ToArray();
    }
}

Usage example:

static Entry[] loadEntries()
{
    const string resourceName = "DefaultNamespace.someData.tsv";
    var ass = Assembly.GetExecutingAssembly();
    using var stm = ass.GetManifestResourceStream( resourceName )
        ?? throw new ApplicationException( "Embedded resource is missing" );
    return Entry.loadTabSeparated( stm );
}

If the text file is at least 100kb, and you want to save size of the binary, compress that resource file into GZip. The C# code only needs a single line of code to decompress on the fly, see GZipStream class.

Also, since that data is immutable, you probably should not load it on every use, instead load it once, and keep the array in memory for as long as your app is running. For instance, you could cache in a static readonly field of some class.

  • Related