A Discussion on ADO.NET

Published Sun, Jun 27 2004 12:27 | William

Those of you who know me no doubt know that I'm an ADO.NET nut.  To date, I can honestly say that i've read every ADO.NET book out there, at least as indicated by Amazon.com and the major publishers.  I'm pretty active in the MS ADO.NET Ng (not the last two weeks but other than that...) and it's nothing for me to put up 900+ posts/answers in a month.  Greg Low had  this interesting take on Bill Vaughn's ADO & ADO.NET Examples and Best Practices for VB Programmers (he's also got the same book done in C# with Peter Blackburn- although the title is changed to “...For C# Programmers”) book.  I'm a big fan of Vaughn and read everything he puts out.  From the looks of his pieces i've gotten on his new Sql Server Reporting Services book, he lives up to expectations again.  Anyway, Greg mentions that he has a few differing opinions with Vaughn on the use of the .GetOrdinal() method of the DataReader as well as some other issues on DataReaders in general and Typed Datasets.  He didn't get into what they were but I asked him to elaborate on what those were b/c they'll surely be interesting.  Anyway, if you aren't familiar with it it breaks down like this.

If you use the Column Name with a datareader ie dataReaderName.Getxxx(”ColumnName”) then the runtime needs to resolve the Column's index at each pass.  Since DataReaders are usually intended to high performance data access, you want to squeeze out every bit of performance you can get.  However, using Getxxx(0) isnt' the most readable thing in the world, even if you document what 0 is.  So the reader has a GetOrdinal method which lets you do something like  int iClearColumnName = dataReaderName.GetOrdinal(”ColumnName”);  and then you can reference it in code like this :  readerName.Getxxx(iClearColumnName) - hence giving you the best of both worlds.  Vaughn's contention is that GetOrdinal has some overhead with it and you already know the index of the column name by virtue of the SQL Statement.  To those that would say that you don't if for instance you are using Stored procs, well, you would have the know the column's name anyway so you should, or can easily find out, the position. As such, you can create an enum with each of the colum names, reference those the same way you would using getordinal and have the best of both worlds without any overhead associated with GetOrdinal.  I'm a performance freak - but as I mature I realize that readability is every bit (and often more) important.  GetOrdinal seems like a great compromise but Vaughn's idea seems even better.  I'm not sure if this is Greg's issue but I'm dying to find out.

As far as typed DataSets... well, this seems like  an open and shut case.  If you know the structure of the query in advance, USE Them.  They are faster, cleaner, have intellisense support etc.  If you don't know the structure, then you have no choice, you have to use an untyped one (technically this isnt' true but the workaround is more work than it's worth in my estimation so I wont' get into it).  I'm also intrigued to see what Greg's viewpoint is here. 

If you spend as much time studying something as I have ADO.NET (and making mistakes and mistakes and mistakes), it's easy to think your opinions are 'right' - hell I know a lot of people that are ignorant as hell on a lot of things and they think their opinions are facts. But to truly know something, it certainly helps if you know every angle on the thing, the upsides and downsides from every perspective.  As such, I'm really intrigued in what Greg has to say - Or any of you for that matter.

BTW, if you need any recommendations on ADO.NET books (or .NET books in general), I can probably be of assistance.  My man Phil has just given me some great recommendations on Patterns and although Borders didn't have all of them, I'm going to pick all of them up - I got three of them already.  Patterns is defintely something I'm not that strong on - something I intend to fix ASAP.  I've also been doing a LOT of work with ADO.NET 2.0 and trust me, there's a lot.  So far, I don't think it's a stretch to say that learning ADO.NET 2.0 if you know 1.1 is about as much work as learning ADO.NET was from ADO. 2.x .  Much is the same, but there are a ton of new features (I'll write more tonight) and there's a LOT of thinking you should engage in before hacking away at it - then again, what else is new?

Comments

# William said on June 28, 2004 10:15 AM:

Hey bill,

again, an excellent overview of ADO.NET's plain vanilla datareader object - though on one issue i simply cannot hold back...typed datasets are EASIER to create than nearly anything else..seriously..make the tools work for you...by using VS.Net and the inbuilt DataForm Wizard you can be up and running with typed datasets in less than 5 minutes, and that would include seperation of dataaccess, xsd creation and two-three small methods to fill the dataset..

i wrote (as a virgin article writer, lacking technical flair) a small how-to/article on how to create typed datasets in less time than it takes to have sex....now, mind you - i'm definitely not the suave writers you and phil are so don't go chopping my balls of for this one : http://weblogs.dotnetforum.dk/deprecated/articles/typed_datasets.aspx << my first attempt to convert all heathens to typed datasets...also, it was written a little while ago, before i really got into it...comment if you like or slag the shite out of me for ruining a perfectly stable technology.

# William said on June 28, 2004 11:24 AM:

Thanks! I just checked out your article, it's great, give yourself some credit!

# William said on June 28, 2004 11:55 AM:

Bill:

thanks for the feedback - i'm a great article writer..he hheeh - hopefully all heathens will see the light one day and notice just how wonderfull easy typed datasets really are to work with.

# William said on June 28, 2004 1:53 PM:

I'll be satisfied when they notice that you need a constant connection when using a dataReader and that when a DataSet doesn't HasChanges, then calling update isn't going to do anyting.

# William said on June 28, 2004 4:23 PM:

Cool, Thanks for that little bit on using the column index rather than the column name when using a DataReader...I was using the name to make my code more readable, but now I'll create an enum for each query I have. Could I also use constants to hold the index of the column instead of creating an enum? I only ask because I've got 30+ queries in my app and creating 30+ enums would be a pain...come to think of it, creating 100+ constants would be a bigger pain in the a55!

As for the typed DataSets, can those be created with an Oracle database? I'm sure I could but I've not really found any info at Oracle's website (is it me or does their Oracle 9i Lite information suck the big one at MetaLink and OTN?) Can I add my Oracle database to the server explorer in VS.NET even though my database is on another server? I've never tried using typed DataSets, I create the sql statement as a string, add it to the command object, and throw it at the connection object. (yes I use parameters so don't kill me Bill - that's almost a movie)

# William said on June 28, 2004 5:52 PM:

Skicow:

You can definitely use Typed Datasets with Oracle - or no database at all for that matter. They exist in System.Data .. so they are independent of any implementation. DataReaders on the other hand, inherit from IDataReader and are implemented through the respective client provider libraries. You can (should be able to anyway) add your Oracle db through VS.NET, if you have any problems, let me know and I'll walk you through it.

Under your scenario above, using GetOrdinal may make more sense, but if you can, opt for Stored Procs too - that will make part of it easier. If you don't change your column names very often, you won't have any issues. I've found that you will change aliases (which will break named references) more than you change positions so the enum method can be the most robust depending on the circumstances.

P.S. I'm glad I got the reputation for screamign about Parameters. It's probably ok not to use them here and there, but as soon as you say that, every VB6 programmer out there decides that he wants to write his new ASP.NEt app the 'old way he used to do things" and use dynamic sql and Access - and then complain when everything breaks. Glad to hear your using Params ;-)

# William said on June 28, 2004 6:29 PM:

i like vaugn too...

i'm definitely not a ADO.NET guru or anything like that, and as for my own personal preference i just use the indexer.

however, i'm surprised that code maintenance hasn't been mentioned. we know that the using the column names certainly makes the code easier to read, but another strong factor it has in it's favor is when you need to add or remove a field to your stored proc, sql statement, etc... when this happens all you indexers are moved, and you've now more or less broke your app. you'll need to go back and touch all the code that uses the indexers. if it's a large app, that can be a real pain in the ass and a maintenance nightmare. generally most of the code doing that shit anyway should be localized into your data access layer or objects, but if your developing against a database that doesn't have it's schema fixed yet, GetOrdinal and column names make sense. after all, once the database has been fixed, you can always just switch over to the indexers if you need to...

and we all know what they say about premature optimization...

# William said on June 29, 2004 8:32 AM:

While you all make some very GOOD points, ones I agree with totally, two things become clear:
1) No magic bullets - what works well in one approach may be a disaster in another
2) Performance isn't everything - we often lose track of this in the fight between maintainability nuts and performance nuts, and I've fallen into both sides. Balance is the solution
3) Phil's reference to premature optimizations is Priceless. They should be done at the end, when everything else is in place. The more you change, the more you change (boy am I smart) and that means a lot more potential problems.

But my blood is getting hot after a few NG answers I gave last night, I'm getting really close to shoving a Concatenated Dynamic SQL String (without parameters) up some VB programmer's ass if he keeps posting about problems he's having with it and access. THEY aren't well suited for the web you iditot, get over it.

Anyway, great discussion guys.

# William said on June 29, 2004 9:04 AM:

I agree with Phil and Bill, there is more than one way to skin a cat (am I going to get in trouble with the SPCA for saying that?) but there is definately more wrong ways to do something then right ways. And that's why it's good for me to listen and learn from all you guys. I've only been programming in VB.NET for about 6 months, and before that it was about 6 months of eVB, and before that about 5 years of PL/SQL. Sure I took OOP in school, but the difference between school knowledge and real world knowledge is insane...I was doing what Bill is getting pissed about in his previous comment of this post a few months ago, 'Concatenated Dynamic SQL String (without parameters)', left over from my eVB days, but now I'm using StringBuilder and Parameters, and my queries have sped up about 50% in my PPC 2003 app.

Thanks for the offer of help with getting my Oracle db into VS Bill, I'll try it out myself and if I can't get it I'll give you a yell.

# William said on June 29, 2004 9:36 AM:

Skicow:

As Mr. Caustic pointed out you can use typed datasets with anything..typed datasets gets their schemas from XSD files (XML schemas) so as long as you fill it accordingly you can put any source to it. The .Net framework uses XML as an underlying structure/data container for most of it's work...that's what makes it such a beauty to work with.

i even managed to run it against MySQL (ok, shame on me, but it's a work hazard as we do use it at work!!!)..

# William said on June 29, 2004 12:20 PM:

Bill & Co...

for your info.. http://lab.msdn.microsoft.com/express << Visual Studio 2005 Express Edition...beta though, but nice concept!..

Now, to simplify my life and install ASP.Net 2.0...perfect!

# William said on July 1, 2004 1:52 PM:

Thanks Brian!

# William said on February 23, 2005 4:40 AM:

Hello Sir,
How i can get the actual Column Name using dataset or table row???


Kapil

# William said on March 18, 2005 5:59 PM:

hello,
i've been searching for differences between ADO.net V 1.0 and ADO.net V 1.1... but i couldn't find any information related... now, as you know, version 2.0 is the latest and it's getting all the attention.

could you please tell me the differences between ADO.net V 1.0 and ADO.net V 1.1?

thanks a lot.

Diego Ricardo Rojas
e-mail: ldereck58@gmail.com

Search

This Blog

Tags

Community

Archives

News

My other sites

Cool Stuff

Book Stuff

Security

ORM

Data Access

Funny Stuff

Compact Framework Stuff

Web Casts

My KnowledgeBase Articles

My MVP Profile

Design Patterns

Performance

Debugging

Remoting

My Fellow Authors

My Books

LINQ

Misc

Speech

Syndication

Email Notifications