Best way to store data locally in .NET (C#)

后端 未结 19 1247
南方客
南方客 2020-11-28 01:53

I\'m writing an application that takes user data and stores it locally for use later. The application will be started and stopped fairly often, and I\'d like to make it save

相关标签:
19条回答
  • 2020-11-28 02:09

    If your data is complex, high in quantity or you need to query it locally then object databases might be a valid option. I'd suggest looking at Db4o or Karvonite.

    0 讨论(0)
  • 2020-11-28 02:09

    Without knowing what your data looks like, i.e. the complexity, size, etc...XML is easy to maintain and easily accessible. I would NOT use an Access database, and flat files are more difficult to maintain over the long haul, particularly if you are dealing with more than one data field/element in your file.

    I deal with large flat-file data feeds in good quantities daily, and even though an extreme example, flat-file data is much more difficult to maintain than the XML data feeds I process.

    A simple example of loading XML data into a dataset using C#:

    DataSet reportData = new DataSet();
    
    reportData.ReadXml(fi.FullName);
    

    You can also check out LINQ to XML as an option for querying the XML data...

    HTH...

    0 讨论(0)
  • 2020-11-28 02:10

    Just finished coding data storage for my current project. Here is my 5 cents.

    I started with binary serialization. It was slow (about 30 sec for load of 100,000 objects) and it was creating a pretty big file on the disk as well. However, it took me a few lines of code to implement and I got my all storage needs covered. To get better performance I moved on custom serialization. Found FastSerialization framework by Tim Haynes on Code Project. Indeed it is a few times faster (got 12 sec for load, 8 sec for save, 100K records) and it takes less disk space. The framework is built on the technique outlined by GalacticJello in a previous post.

    Then I moved to SQLite and was able to get 2 sometimes 3 times faster performance – 6 sec for load and 4 sec for save, 100K records. It includes parsing ADO.NET tables to application types. It also gave me much smaller file on the disk. This article explains how to get best performance out of ADO.NET: http://sqlite.phxsoftware.com/forums/t/134.aspx. Generating INSERT statements is a very bad idea. You can guess how I came to know about that. :) Indeed, SQLite implementation took me quite a bit of time plus careful measurement of time taking by pretty much every line of the code.

    0 讨论(0)
  • 2020-11-28 02:12

    I recommend XML reader/writer class for files because it is easily serialized.

    Serialization in C#

    Serialization (known as pickling in python) is an easy way to convert an object to a binary representation that can then be e.g. written to disk or sent over a wire.

    It's useful e.g. for easy saving of settings to a file.

    You can serialize your own classes if you mark them with [Serializable] attribute. This serializes all members of a class, except those marked as [NonSerialized].

    The following is code to show you how to do this:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing;
    
    
    namespace ConfigTest
    { [ Serializable() ]
    
        public class ConfigManager
        {
            private string windowTitle = "Corp";
            private string printTitle = "Inventory";
    
            public string WindowTitle
            {
                get
                {
                    return windowTitle;
                }
                set
                {
                    windowTitle = value;
                }
            }
    
            public string PrintTitle
            {
                get
                {
                    return printTitle;
                }
                set
                {
                    printTitle = value;
                }
            }
        }
    }
    

    You then, in maybe a ConfigForm, call your ConfigManager class and Serialize it!

    public ConfigForm()
    {
        InitializeComponent();
        cm = new ConfigManager();
        ser = new XmlSerializer(typeof(ConfigManager));
        LoadConfig();
    }
    
    private void LoadConfig()
    {     
        try
        {
            if (File.Exists(filepath))
            {
                FileStream fs = new FileStream(filepath, FileMode.Open);
                cm = (ConfigManager)ser.Deserialize(fs);
                fs.Close();
            } 
            else
            {
                MessageBox.Show("Could not find User Configuration File\n\nCreating new file...", "User Config Not Found");
                FileStream fs = new FileStream(filepath, FileMode.CreateNew);
                TextWriter tw = new StreamWriter(fs);
                ser.Serialize(tw, cm);
                tw.Close();
                fs.Close();
            }    
            setupControlsFromConfig();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    After it has been serialized, you can then call the parameters of your config file using cm.WindowTitle, etc.

    0 讨论(0)
  • 2020-11-28 02:15

    The first thing I'd look at is a database. However, serialization is an option. If you go for binary serialization, then I would avoid BinaryFormatter - it has a tendency to get angry between versions if you change fields etc. Xml via XmlSerialzier would be fine, and can be side-by-side compatible (i.e. with the same class definitions) with protobuf-net if you want to try contract-based binary serialization (giving you a flat file serializer without any effort).

    0 讨论(0)
  • 2020-11-28 02:15

    It depends on the amount of data you are looking to store. In reality there's no difference between flat files and XML. XML would probably be preferable since it provides a structure to the document. In practice,

    The last option, and a lot of applications use now is the Windows Registry. I don't personally recommend it (Registry Bloat, Corruption, other potential issues), but it is an option.

    0 讨论(0)
提交回复
热议问题