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文件,我应该看哪些类可以轻松操作?
9 个解决方案
#1
16
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();
}
#2
13
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,如果你后来确定你真的需要高性能的关系查询逻辑,它将是一个平滑过渡到数据库。
#3
7
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 question for sample code.
在.NET中访问和读取数据库非常简单 - 请查看此问题以获取示例代码。
#4
4
SQL Express from 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 Framework that shipped with .NET 3.5 SP1
或者,您可以简单地在应用程序中创建数据集并将它们序列化为xml,或者您可以使用.NET 3.5 SP1附带的新创建的实体框架之类的东西。
#5
3
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或二进制文件 - 选择取决于您是否希望它是人类可读的)。
#6
2
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 Edition或VistaDB。两者都在您的应用程序中托管在proc中。
#7
1
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 Compact,SQLite等...)
- Scott Guthrie on LINQ
- Summer of nHibernate
LINQ的Scott Guthrie
nHibernate的夏天
#8
1
You can either use the System::Xml
namespace or the System::Data
namespace. The first gives you raw XML, the latter gives you a handy wrapper to the XML.
您可以使用System :: Xml命名空间或System :: Data命名空间。第一个为您提供原始XML,后者为您提供了一个方便的XML包装器。
#9
1
For this situation, the [Serializable]
attribute on a nicely modelled Stats
class and XmlSerializer
are the way to go, IMO.
对于这种情况,一个很好建模的Stats类和XmlSerializer上的[Serializable]属性是要走的路,IMO。
#1
16
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();
}
#2
13
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,如果你后来确定你真的需要高性能的关系查询逻辑,它将是一个平滑过渡到数据库。
#3
7
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 question for sample code.
在.NET中访问和读取数据库非常简单 - 请查看此问题以获取示例代码。
#4
4
SQL Express from 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 Framework that shipped with .NET 3.5 SP1
或者,您可以简单地在应用程序中创建数据集并将它们序列化为xml,或者您可以使用.NET 3.5 SP1附带的新创建的实体框架之类的东西。
#5
3
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或二进制文件 - 选择取决于您是否希望它是人类可读的)。
#6
2
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 Edition或VistaDB。两者都在您的应用程序中托管在proc中。
#7
1
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 Compact,SQLite等...)
- Scott Guthrie on LINQ
- Summer of nHibernate
LINQ的Scott Guthrie
nHibernate的夏天
#8
1
You can either use the System::Xml
namespace or the System::Data
namespace. The first gives you raw XML, the latter gives you a handy wrapper to the XML.
您可以使用System :: Xml命名空间或System :: Data命名空间。第一个为您提供原始XML,后者为您提供了一个方便的XML包装器。
#9
1
For this situation, the [Serializable]
attribute on a nicely modelled Stats
class and XmlSerializer
are the way to go, IMO.
对于这种情况,一个很好建模的Stats类和XmlSerializer上的[Serializable]属性是要走的路,IMO。