tags:

views:

3047

answers:

10

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.

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?

+6  A: 

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.

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

palmsey
+10  A: 

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.

Greg Hurlman
+2  A: 

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

DannySmurf
+3  A: 

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.

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

Jon Dewees
+2  A: 

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.

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

Justin Bennett
A: 

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.

Skizz

Skizz
+11  A: 

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.


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();
}
Brian Ensink
A: 

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.

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

Dale Ragan
No idea why you've been down-voted so much. In this case, I wouldn't myself go to a database (even the lightweight ones you have described), but your point is valid and one day the original poster may have added enough to his stats to make a DB a more usable option (+1 to try and uncondemn this)
Ray Hayes
A: 

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

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

Will
A: 

For this situation, the [Serializable] attribute on a nicely modeled Stats class and XmlSerializer are the way to go, IMO.

Chris Marasti-Georg