I want to make Cookbook application for storing and reading (and updating) recipes, or anything else to practice OOP programming and thinking. But, I am not sure, what way of storing data, in this case, recipes, is the best in c# (Visual Studio Express). I want to optimize saving and loading data in program, but I have no experience. What is the best way? Is it through XML, SQL, or just plain TXT? Or some other way?
相关问题
- how to define constructor for Python's new Nam
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- Keeping track of variable instances
If you haven't done it yet it would be best to start with XML file input/output before getting into anything too advanced.
Normally you would read and write to files by using the following method to get the path:
So if you want to store your data in a folder called "Cookbook" and a file called "recipes.xml" you could do the following:
This gives you a path like
C:\Users\John\AppData\Local\Cookbook\recipes.xml
or something similar which you can pass to file input and output functions.Then you can get started with the
System.IO
namespace classes likeFile
andFileStream
to learn how to properly open and read/write to files.Then the next higher level step is to pass these file streams to something used to read and write XML to objects, such as Linq to XML (the
XDocument
class) which is the preferred approach. Or the olderXmlSerializer
.Edit:
Here's some sample code to create an object and save it to an XML file:
You would just need to break this code out into a structure that works for you. For example, if you were making a Windows Forms application then the
RecipeBook
would be a private member variable of your main form. In the constructor you could construct therecipesFileFullPath
string and store it as a private member variable too. On the Form.Loaded event you could check if the XML file already exists and if so load it. If not you would create a newRecipeBook
class that's empty. You would also probably only serialize and save when the user clicks a save button or when the Form.Closing event is raised.EDIT:
To deserialize and read from a file you can do the following:
try to use SQLite it's lightweight portable database http://www.sqlite.org/
It depends a lot of how much data the application will hold and how you want to search for that data.
For smaller amounts of data a plain text file containing XML och JSON will do. For larger amounts of data i recommend SQL server or a NOSQL-database (MongoDB or Raven).
This depends on a lot of things, mostly how many recipes you plan on having and if you want to be able to search, sort, or style the recipes.
Text files are the simplest way. You could add some simple search and ordering functionality.
If it's in this hundreds, you could get by with using xml and a serializer. It would allow you to later style the recipes and it gives the data some structure. If your recipes enter the thousands, this way may become a nightmare.
The best way would be a database. Sql Server Compact is a free option that links up nicely with c# through something like Entity Framework. This option will take the longest by far if you're not already familiar with databases or ado.net or entity framework.
I would suggest using a localDB in SqlExpress.
http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx
http://technet.microsoft.com/en-us/library/hh510202.aspx
You could also look at this similar question.
Here as an article on XML vs. DB which shows XML is losing ground.
Your best bet would be to use Sql Server Compact (http://www.microsoft.com/en-us/sqlserver/editions/2012-editions/compact.aspx1).
That is because it comes embedded with your application and you don't need to install extra libraries on deployments etc. Also it is a good storage point for simple tabular data.