存储来自 C# 应用程序的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:04:19  来源:igfitidea点击:

Store data from a C# application

提问by Justin Bennett

I've recently taken up learning some C# and wrote a Yahtzee clone. My next step (now that the game logic is in place and functioning correctly) is to integrate some method of keeping stats across all the games played.

我最近开始学习一些 C# 并编写了一个 Yahtzee 克隆。我的下一步(现在游戏逻辑已经到位并且运行正常)是集成一些方法来保存所有玩过的游戏的统计数据。

My question is this, how should I go about storing this information? My first thought would be to use a database and I have a feeling that's the answer I'll get... if that's the case, can you point me to a good resource for creating and accessing a database from a C# application?

我的问题是,我应该如何存储这些信息?我的第一个想法是使用数据库,我有一种感觉,那就是我会得到的答案......如果是这样的话,你能给我指出一个很好的资源来从 C# 应用程序创建和访问数据库吗?



Storing in an XML file actually makes more sense to me, but I thought if I suggested that I'd get torn apart ;). I'm used to building web applications and for those, text files are generally frowned upon.

存储在 XML 文件中对我来说实际上更有意义,但我想如果我建议我会被撕裂;)。我习惯于构建 Web 应用程序,对于这些应用程序,文本文件通常不受欢迎。

So, going with an XML file, what classes should I be looking at that would allow for easy manipulation?

那么,对于 XML 文件,我应该查看哪些类以便于操作?

采纳答案by Brian Ensink

Here is one idea: use Xml Serialization. Design your GameStats data structure and optionally use Xml attributes to influence the schema as you like. I like to use this method for small data sets because its quick and easy and all I need to do is design and manipulate the data structure.

这是一个想法:使用 Xml 序列化。设计您的 GameStats 数据结构,并有选择地使用 Xml 属性来影响您喜欢的架构。我喜欢将这种方法用于小数据集,因为它快速简便,我需要做的就是设计和操作数据结构。


using (FileStream fs = new FileStream(....))
{
    // Read in stats
    XmlSerializer xs = new XmlSerializer(typeof(GameStats));
    GameStats stats = (GameStats)xs.Deserialize(fs);

    // Manipulate stats here ...

    // Write out game stats
    XmlSerializer xs = new XmlSerializer(typeof(GameStats));
    xs.Serialize(fs, stats);

    fs.Close();
}

回答by palmsey

A database may be overkill - have you thought about just storing the scores in a file?

数据库可能有点矫枉过正——你有没有想过只将分数存储在一个文件中?

If you decide to go with a database, you might consider SQLite, which you can distribute just like a file. There's an open source .NET provider - System.Data.SQLite- that includes everything you need to get started.

如果您决定使用数据库,您可以考虑SQLite,您可以像分发文件一样分发它。有一个开源 .NET 提供程序 - System.Data.SQLite- 包括您开始所需的一切。

Accessing and reading from a database in .NET is quite easy - take a look at this questionfor sample code.

在 .NET 中访问和读取数据库非常简单 - 请查看此问题的示例代码。

回答by Greg Hurlman

A database would probably be overkill for something like this - start with storing your information in an XML doc (or series of XML docs, if there's a lot of data). You get all that nifty XCopy deployment stuff, you can still use LINQ, and it would be a smooth transition to a database if you decided later you really needed performant relational query logic.

对于这样的事情,数据库可能会有些矫枉过正——首先将您的信息存储在 XML 文档(或一系列 XML 文档,如果有大量数据)中。您获得了所有漂亮的 XCopy 部署内容,您仍然可以使用 LINQ,如果您稍后决定确实需要高性能的关系查询逻辑,这将是向数据库的平滑过渡。

回答by TheSmurf

I don't know if a database is necessarily what you want. That may be overkill for storing stats for a simple game like that. Databases are good; but you should not automatically use one in every situation (I'm assuming that this is a client application, not an online game).

我不知道数据库是否一定是您想要的。为这样的简单游戏存储统计数据可能有点过头了。数据库很好;但是您不应该在每种情况下都自动使用一个(我假设这是一个客户端应用程序,而不是一个在线游戏)。

Personally, for a game that exists only on the user's computer, I would just store the stats in a file (XML or binary - choice depends on whether you want it to be human-readable or not).

就个人而言,对于仅存在于用户计算机上的游戏,我只会将统计信息存储在一个文件中(XML 或二进制 - 选择取决于您是否希望它是人类可读的)。

回答by Jon Dewees

SQL Expressfrom MS is a great free, lightweight version of their SQL Server database. You could try that if you go the DB route.

MS 的SQL Express是其 SQL Server 数据库的免费、轻量级版本。如果你走 DB 路线,你可以试试。

Alternatively, you could simply create datasets within the application and serialize them to xml, or you could use something like the newly minted Entity Frameworkthat shipped with .NET 3.5 SP1

或者,您可以简单地在应用程序中创建数据集并将它们序列化为 xml,或者您可以使用诸如.NET 3.5 SP1 附带的新创建的实体框架之类的东西

回答by Skizz

You can either use the System::Xmlnamespace or the System::Datanamespace. The first gives you raw XML, the latter gives you a handy wrapper to the XML.

您可以使用System::Xml命名空间或System::Data命名空间。第一个为您提供原始 XML,后者为您提供一个方便的 XML 包装器。

回答by Dale Ragan

I would recommend just using a database. I would recommend using LINQ or an ORM tool to interact with the database. For learning LINQ, I would take a look at Scott Guthrie's posts. I think there are 9 of them all together. I linked part 1 below. If you want to go with an ORM tool, say nhibernate, then I would recommend checking out the Summer of nHibernate screencasts. They are a really good learning resource for nhibernate.

我建议只使用数据库。我建议使用 LINQ 或 ORM 工具与数据库进行交互。对于学习 LINQ,我会看看 Scott Guthrie 的帖子。我认为他们总共有9个。我在下面链接了第 1 部分。如果您想使用 ORM 工具,比如 nhibernate,那么我建议您查看夏天的 nHibernate 截屏视频。它们是 nhibernate 的一个非常好的学习资源。

I disagree with using XML. With reporting stats on a lot of data, you can't beat using a relational database. Yeah, XML is lightweight, but there are a lot of choices for light weight relational databases also, besides going with a full blown service based implementation. (i.e. SQL Server Compact, SQLite, etc...)

我不同意使用 XML。通过报告大量数据的统计信息,您无法使用关系数据库。是的,XML 是轻量级的,但对于轻量级关系数据库也有很多选择,除了基于成熟的服务实现。(即SQL Server CompactSQLite等...)

回答by Dale Ragan

I'd recommend saving your data in simple POCOs and either serializing them to xml or a binary file, like Brian did above.

我建议将您的数据保存在简单的 POCO 中,并将它们序列化为 xml 或二进制文件,就像上面的 Brian 所做的那样。

If you're hot for a database, I'd suggest Sql Server Compact Edition, or VistaDB. Both are hosted inproc within your application.

如果您对数据库很感兴趣,我建议您使用Sql Server Compact EditionVistaDB。两者都托管在您的应用程序中。

回答by Chris Marasti-Georg

For this situation, the [Serializable]attribute on a nicely modelled Statsclass and XmlSerializerare the way to go, IMO.

对于这种情况,[Serializable]一个很好建模的Stats类上的属性XmlSerializer是要走的路,IMO。