views:

82

answers:

4

I have a C# data structure which consists of classes and collections which have references to each other. Currently I am using Serialization to store the structure in XML, which works as a handy way to save/load the structure into my application.

If I want to be able to save/load the structure into a database, is there a simple way? Should I use LINQ?

A: 

Have you tried NHibernate?

EDIT: I don't know if this would be ok for you, but if you can define DB tables to hold your data structures, you could have code auto-generated using LINQ to SQL. Check ScottGu's tutorial on LINQ to SQL. If your data structures are not changing too often this could be a good way to go.

Paolo Tedesco
Not yet. I'll take a look thanks. I was wondering if there was a built-in way of doing this - like the XML serializer - rather than using third party code?
Caustix
+2  A: 

Just to be 100% clear LINQ has nothing to do with storing data in a database. LINQ is a language query for c# with syntax that resembles SQL.

So in order to answer your question you could look at the Entity Framework (I'd recommend this if you are using .NET 4.0) or LINQ to SQL

Kane
Thank for that. I've just been reading about LINQ and realised the same (and realised that my question now looks really noobish!). I'll check out the Entity Framework..
Caustix
The whole naming of LINQ and LINQ to *blah* is very confusing. Microsoft could have done better with their naming.
Kane
+1 for the Entity Framework
Paolo Tedesco
A: 

If you are using Oracle as a backend database. You can serilize your object graph using BinaryFormatter into a memorystream and strore bytes using BLOB datatype in Oracle Database.

saurabh
I'm currently using XML serialization with the IsReference proeprties set to true, which so far does the trick with minimum lines of code. I was wondering if there was something as easy as this to store the data in a database? I'll probably be using a SQL databse, but I'll check out how BLOB works..
Caustix
As XML is basically string data I would use CLOB/NCLOB instead of BLOB in Oracle.
jmservera
+1  A: 

It currently depends on your needs, and the db engine you are using:

  • If you need to perform queries against the XML contents you can use a XML field (mssql and oracle do support them).
  • If you only need to store/retrieve it, just store it in a long string field (NText in sqlserver. NCLob in Oracle).
jmservera
Thanks for this. I'll read up about XML fields. I'll mark an answer once I've finished all this reading!
Caustix