none
Best way to store data and retrieve it quickly(!) with C#? RRS feed

  • Question

  • I'm writing an application that will be using a lookup table when it runs. The table will have information that looks approximately like this:

                Score,     Trials,     Hits,     Misses

    cody     7687     67     767         67
    albert     7678     677     7000     778
    joe         675     786     767         676

    What is the best way to store and retrieve this data? My biggest priority is for the data to be as fast as possible to retrieve. If it matters I only need to be able to search for the names (like "cody") and retrieve their associated values.

    Thanks on beforehand for a reply
    Monday, July 2, 2007 9:13 PM

All replies

  • Code Snippet

    using System;

    using System.IO;

    using System.Xml;

    using System.Collections;

    using System.Collections.Generic;

    using System.Xml.Serialization;

    using System.ComponentModel;

     

    [Serializable()]

    public class Record : IComparable<Record>

    {

    private int m_Trials;

    private int m_Score;

    private int m_Hits;

    private int m_Misses;

    private string m_Name;

     

    [XmlElement()]

    public string Name {

    get { return this.m_Name; }

    set { this.m_Name = value; }

    }

    [XmlElement()]

    public int Hits {

    get { return this.m_Hits; }

    set { this.m_Hits = value; }

    }

    [XmlElement()]

    public int Misses {

    get { return this.m_Misses; }

    set { this.m_Misses = value; }

    }

    [XmlElement()]

    public int Score {

    get { return this.m_Score; }

    set { this.m_Score = value; }

    }

    [XmlElement()]

    public int Trials {

    get { return this.m_Trials; }

    set { this.m_Trials = value; }

    }

     

    int IComparable<Record>.CompareTo(Record other)

    {

    return this.Score.CompareTo(other.Score);

    }

    }

     

    [XmlRoot("Scores")]

    public class ScoreCollection : List<Record>

    {

    }

     

    public class ScoresManager

    {

    public ScoreCollection Load(string fileName)

    {

    ScoreCollection scoreList = new ScoreCollection();

    if (string.IsNullOrEmpty(fileName))

    {

    return scoreList;

    }

    if (!File.Exists(fileName))

    {

    return scoreList;

    }

    try {

    XmlSerializer s = new XmlSerializer(typeof(ScoreCollection));

    TextReader sr = new StreamReader(fileName);

    scoreList = (ScoreCollection)s.Deserialize(sr);

    if ((sr != null))

    {

    sr.Close();

    }

    }

    catch {

    scoreList = new ScoreCollection();

    }

    if ((scoreList.Count > 0))

    {

    scoreList.Sort();

    }

    return scoreList;

    }

     

    public bool Save(string fileName, ScoreCollection scoreList)

    {

    if (((scoreList == null) || string.IsNullOrEmpty(fileName)))

    {

    return false;

    }

    if ((scoreList.Count > 0))

    {

    scoreList.Sort();

    try {

    XmlSerializer s = new XmlSerializer(typeof(ScoreCollection));

    TextWriter sw = new StreamWriter(fileName);

    s.Serialize(sw, scoreList);

    if ((sw != null))

    {

    sw.Close();

    }

    }

    catch {

    return false;

    }

    return true;

    }

    else

    {

    return false;

    }

    }

    }

     

    If you have any questions, feel free to post them.

    Tuesday, July 3, 2007 7:35 AM
  • First define a class to store the data:

    Code Snippet

    class Facts
    {
     string name;
     int score;
     int trials;

     // add properties to access the data
    }



    Since you want to access the Facts by a key, more specific by the name, you could store them in a Dictionary<string, Facts> as following:

    Code Snippet

    Dictionary<string, Facts> facts = new Dictionary<string, Facts>();
    Facts fact1 = new Facts();
    fact1.Name = "Tim";
    fact1.Score = 7687;
    ...
    facts.Add(fact1);




    Now you can lookup facts (or try to access them):

    Code Snippet

    Facts fact2 = facts["Tim"];

    Facts fact3;
    if (fact.TryGetValue("Jef", out fact3))
    {
    // i found a Fact with Name == "Jef" and it's in fact3
    }



    Tuesday, July 3, 2007 8:18 AM