Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/load the data on application start/end.

It'd be fairly straightforward if I used flat files, as the data doesn't really need to be secured (it'll only be stored on this PC). The options I believe are thus:

  • Flat files
  • XML
  • SQL DB

Flat files require a bit more effort to maintain (no built in classes like with XML), however I haven't used XML before, and SQL seems like overkill for this relatively easy task.

Are there any other avenues worth exploring? If not, which of these is the best solution?


Edit: To add a little more data to the problem, basically the only thing I'd like to store is a Dictionary that looks like this

Dictionary<string, List<Account>>  

where Account is another custom type.

Would I serialize the dict as the xmlroot, and then the Account type as attributes?


Update 2:

So it's possible to serialize a dictionary. What makes it complicated is that the value for this dict is a generic itself, which is a list of complex data structures of type Account. Each Account is fairly simple, it's just a bunch of properties.

It is my understanding that the goal here is to try and end up with this:

<Username1>     <Account1>         <Data1>data1</Data1>         <Data2>data2</Data2>     </Account1> </Username1> <Username2>     <Account1>         <Data1>data1</Data1>         <Data2>data2</Data2>     </Account1>     <Account2>         <Data1>data1</Data1>         <Data2>data2</Data2>     </Account2>  </Username2> 

As you can see the heirachy is

  • Username (string of dict) >
  • Account (each account in the List) >
  • Account data (ie class properties).

Obtaining this layout from a Dictionary<Username, List<Account>> is the tricky bit, and the essence of this question.

There are plenty of 'how to' responses here on serialisation, which is my fault since I didn't make it clearer early on, but now I'm looking for a definite solution.

like image 454
George Avatar asked Dec 21 '09 19:12

George


People also ask

How can we store data without using database in C#?

Insert the data from the textbox into the Grid View without using any database in ASP. NET using C#. Step 1: To do this firstly open the Microsoft Visual Studio 2010 then click the File->New->Web Site->select the ASP. NET website.

How does computer store data in C#?

The computer and the compiler of C# are responsible for allocating memory locations for program's data. For example using C# coding you can tell the compiler to create a location named x that will store integer value also you can tell it to allocate another location called y that will store another number.


2 Answers

I'd store the file as JSON. Since you're storing a dictionary which is just a name/value pair list then this is pretty much what json was designed for.
There a quite a few decent, free .NET json libraries - here's one but you can find a full list on the first link.

like image 97
zebrabox Avatar answered Sep 29 '22 01:09

zebrabox


It really depends on what you're storing. If you're talking about structured data, then either XML or a very lightweight SQL RDBMS like SQLite or SQL Server Compact Edition will work well for you. The SQL solution becomes especially compelling if the data moves beyond a trivial size.

If you're storing large pieces of relatively unstructured data (binary objects like images, for example) then obviously neither a database nor XML solution are appropriate, but given your question I'm guessing it's more of the former than the latter.

like image 24
Adam Robinson Avatar answered Sep 29 '22 00:09

Adam Robinson