tags:

views:

1641

answers:

12

I've found myself increasingly unsatisfied with the DataSet/DataTable/DataRow paradigm in .Net, mostly because it's often a couple of steps more complicated than what I really want to do. In cases where I'm binding to controls, DataSets are fine. But in other cases, there seems to be a fair amount of mental overhead.

I've played a bit with SqlDataReader, and that seems to be good for simple jaunts through a select, but I feel like there may be some other models lurking in .Net that are useful to learn more about. I feel like all of the help I find on this just uses DataSet by default. Maybe that and DataReader really are the best options.

I'm not looking for a best/worst breakdown, just curious what my options are and what experiences you've had with them. Thanks!

-Eric Sipple

+15  A: 

Since .NET 3.5 came out, I've exclusively used LINQ. It's really that good; I don't see any reason to use any of those old crutches any more.

As great as LINQ is, though, I think any ORM system would allow you to do away with that dreck.

DannySmurf
Do you mean LINQ To SQL (aka L2S)?
rohancragg
Obviously, since we're talking about database access. Though LINQ to Everything Else is just as good.
DannySmurf
+4  A: 

We've moved away from datasets and built our own ORM objects loosely based on CSLA. You can get the same job done with either a DataSet or LINQ or ORM but re-using it is (we've found) a lot easier. 'Less code make more happy'.

hometoast
+1  A: 

I use them extensively but I don't make use of any of the "advanced" features that Microsoft was really pushing when the framework first came out. I'm basically just using them as Lists of Hashtables, which I find perfectly useful.

I have not seen good results when people have tried to make complex typed DataSets, or tried to actually set up the foreign key relationships between tables with DataSets.

Of course, I am one of the weird ones that actually prefers a DataRow to an entity object instance.

Eric Z Beard
+1  A: 

Pre linq I used DataReader to fill List of my own custom domain objects, but post linq I have been using L2S to fill L2S entities, or L2S to fill domain objects.

Once I get a bit more time to investigate I suspect that Entity Framework objects will be my new favourite solution!

KiwiBastard
+2  A: 

I was fed up with DataSets in .Net 1.1, at least they optimised it so that it doesn't slow as exponentially for large sets any more.

It was always a rather bloated model - I haven't seen many apps that use most of its features.

SqlDataReader was good, but I used to wrap it in an IEnumerable<T> where the T was some typed representation of my data row.

Linq is a far better replacement in my opinion.

Keith
A: 

I just build my business objects from scratch, and almost never use the DataTable and especially not the DataSet anymore, except to initially populate the business objects. The advantages to building your own are testability, type safety and intellisense, extensibility (try adding to a DataSet) and readability (unless you enjoy reading things like Convert.ToDecimal(dt.Rows[i]["blah"].ToString())).

If I were smarter I'd also use an ORM and 3rd party DI framework, but just haven't yet felt the need for those. I'm doing lots of smaller size projects or additions to larger projects.

Mike
+1  A: 

DataSets are great for demos.

I wouldn't know what to do with one if you made me use it.

I use ObservableCollection

Then again i'm in the client app space, WPF and Silverlight. So passing a dataset or datatable through a service is ... gross.

DataReaders are fast, since they are a forward only stream of the result set.

Brian Leahy
+1  A: 

Selecting a modern, stable, and actively supported ORM tool has to be probably the single biggest boost to productivity just about any project of moderate size and complexity can get. If you're concluding that you absolutely, absolutely, absolutely have to write your own DAL and ORM, you're probably doing it wrong (or you're using the world's most obscure database).

If you're doing raw datasets and rows and what not, spend the day to try an ORM and you'll be amazed at how much more productive you can be w/o all the drudgery of mapping columns to fields or all the time filling Sql command objects and all the other hoop jumping we all once went through.

I love me some Subsonic, though for smaller scale projects along with demos/prototypes, I find Linq to Sql pretty damn useful too. I hate EF with a passion though. :P

bakasan
+3  A: 

I've been using the Data Transfer Objects pattern (originally from the Java world, I believe), with a SqDataReader to populate collections of DTOs from the data layer for use in other layers of the application. The DTOs themselves are very lightweight and simple classes composed of properties with gets/sets. They can be easily serialized/deserialized, and used for databinding, making them pretty well suited to most of my development needs.

Jesse Taber
A: 

I NEVER use datasets. They are big heavyweight objects only usable (as someone pointed out here) for "demoware". There are lot's of great alternatives shown here.

fuzzbone
"Heavyweight objects"? Does that term really have any meaning?
Kyralessa
Yes there is a significant meaning. Serialize a dataset with even just a few rows to an XML file and take a look how big it is. Then make a simple Data Transfer Object (Jesse's response). Just define a simple class with members for each column - and then a List<DTO> class. Serialize that to an XML file - and take a look at the difference.
fuzzbone
If you're really serializing DataSets, and then passing them over the wire or something, and there's a genuine performance problem, that's one thing. But I've seen "DataSet bloat" used as justification for making hideous, string-literal-laden code that uses DataReaders everywhere in far too many shops I've worked in.
Kyralessa
Certainly replacing one bad implementation with another is a lousy solution - but I've yet to see a solution using dataset's that can't be implemented better as more effectively without one. Datasets are usefull for "DEMOware" and a holdover from pre-.NET ADO
fuzzbone
Now see, that makes me think you haven't worked with ADO.NET DataSets much, and perhaps just don't know much about them at all. ADO.NET DataSets are disconnected; they're containers for data. They enforce things like database relations and nullability, which is why they have so many methods and properties that people refer to as "bloat". I can understand why people wouldn't prefer them as a matter of style, but they're a solid alternative for those who don't mind a more database-oriented (rather than object-model-oriented) style.
Kyralessa
+3  A: 

I'm a huge fan of SubSonic. A well-written batch/CMD file can generate an entire object model for your database in minutes; you can compile it into its own DLL and use it as needed. Wonderful model, wonderful tool. The site makes it sound like an ASP.NET deal, but generally speaking it works wonderfully just about anywhere if you're not trying to use its UI framework (which I'm moderately disappointed in) or its application-level auto-generation tools.

For the record, here is a version of the command I use to work with it (so that you don't have to fight it too hard initially):

sonic.exe generate /server [servername] /db [dbname] /out [outputPathForCSfiles] /generatedNamespace [myNamespace] /useSPs true /removeUnderscores true

That does it every time ... Then build the DLL off that directory -- this is part of an NAnt project, fired off by CruiseControl.NET -- and away we go. I'm using that in WinForms, ASP.NET, even some command-line utils. This generates the fewest dependencies and the greatest "portability" (between related projects, EG).

Note

The above is now well over a year old. While I still hold great fondness in my heart for SubSonic, I have moved on to LINQ-to-SQL when I have the luxury of working in .NET 3.5. In .NET 2.0, I still use SubSonic. So my new official advice is platform version-dependent. In case of .NET 3+, go with the accepted answer. In case of .NET 2.0, go with SubSonic.

John Rudy
+1  A: 

I've used typed DataSets for several projects. They model the database well, enforce constraints on the client side, and in general are a solid data access technology, especially with the changes in .NET 2.0 with TableAdapters.

Typed DataSets get a bad rap from people who like to use emotive words like "bloated" to describe them. I'll grant that I like using a good O/R mapper more than using DataSets; it just "feels" better to use objects and collections instead of typed DataTables, DataRows, etc. But what I've found is that if for whatever reason you can't or don't want to use an O/R mapper, typed DataSets are a good solid choice that are easy enough to use and will get you 90% of the benefits of an O/R mapper.

EDIT:

Some here suggest that DataReaders are the "fast" alternative. But if you use Reflector to look at the internals of a DataAdapter (which DataTables are filled by), you'll see that it uses...a DataReader. Typed DataSets may have a larger memory footprint than other options, but I've yet to see the application where this makes a tangible difference.

Use the best tool for the job. Don't make your decision on the basis of emotive words like "gross" or "bloated" which have no factual basis.

Kyralessa
I'm not sure your comment is valid - a DataSet is populated via a DataAdaptor that uses a DataReader but when is the data available? If you have to wait until the read of all rows is completed, of course it has to be slower.
Andy Dent
It depends on what you're doing with the data, though. Maybe you're processing a large set of data row-by-row; maybe you're operating on the full set and can't do anything till you have it all anyway. Maybe you're getting the equivalent of one row, and it doesn't matter which you use. *Always* eschewing DataSets is premature optimization. And if you need to edit and persist data, DataSets are much easier to work with than DataReaders and temporary objects. To avoid them is to obfuscate your code for no reason.
Kyralessa