What is the best way to store data in c# applicati

2019-02-09 19:15发布

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?

6条回答
劫难
2楼-- · 2019-02-09 19:53

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:

string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

So if you want to store your data in a folder called "Cookbook" and a file called "recipes.xml" you could do the following:

string dataPath = Path.Combine(appDataPath, "Cookbook");
string recipesFileFullPath = Path.Combine(dataPath, "recipes.xml");

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 like File and FileStream 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 older XmlSerializer.

Edit:

Here's some sample code to create an object and save it to an XML file:

public class RecipeBook
{
    public List<Recipe> Recipes { get; set; }

    public RecipeBook()
    {
        Recipes = new List<Recipe>();
    }
}

public class Recipe
{
    public DateTime LastModified { get; set; }
    public DateTime Created { get; set; }
    public string Instructions { get; set; }
}

public void SomeFunction()
{
    RecipeBook recipeBook = new RecipeBook();

    var myRecipe = new Recipe()
    {
        Created = DateTime.Now,
        LastModified = DateTime.Now,
        Instructions = "This is how you make a cake."
    };

    recipeBook.Recipes.Add(myRecipe);

    var doc = new XDocument();
    using (var writer = doc.CreateWriter())
    {
        var serializer = new XmlSerializer(typeof(RecipeBook));

        serializer.Serialize(writer, recipeBook);
    }

    doc.Save(recipesFileFullPath);
}

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 the recipesFileFullPath 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 new RecipeBook 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:

var serializer = new XmlSerializer(typeof(RecipeBook));
using (var fs = new FileStream(recipesFileFullPath))
{
    RecipeBook book = (RecipeBook)serializer.Deserialize(fs);
}
查看更多
够拽才男人
3楼-- · 2019-02-09 19:58

try to use SQLite it's lightweight portable database http://www.sqlite.org/

查看更多
趁早两清
4楼-- · 2019-02-09 19:58

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).

查看更多
smile是对你的礼貌
5楼-- · 2019-02-09 19:58

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.

查看更多
家丑人穷心不美
6楼-- · 2019-02-09 20:10

I would suggest using a localDB in SqlExpress.

Microsoft® SQL Server® 2012 Express is a powerful and reliable free data management system that delivers a rich and reliable data store for lightweight Web Sites and desktop applications.

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.

查看更多
We Are One
7楼-- · 2019-02-09 20:11

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.

查看更多
登录 后发表回答