DataSet discussion revisited...

I want to revisit a discussion that I had early on in my blog - Datasets.

After several months of heavy development (actually a conversion of some ASP code that I have been maintaining), I have found that I don't use the dataset.

Maybe it's the advice that I've been getting or maybe it's the result of my design.  FWIW, here's what my stuff looks like (in VB):

' The following class will handle all the SQL Calls for an area of the site
Class sqlPortionOfSite
inherits sqlBaseClass
Public Function GetData() as CollectionOfMyData
  Dim __dr as sqlDataReader
  Dim __Rtn as CollectionOfMyData

  __Rtn = New CollectionOfMyData()

  Try 
    __dr = sqlHelper.ExecuteDataReader(ConnString, storedproc, "MySP") '   I use the Data Application Block

    While __dr.read() 
      Dim __Data as MyData = New MyData 
      __Data.Field1 = __Dr.GetInteger(__Dr.getOrdinal("Field1")) 
      __Data.Field2 = __Dr.GetString(__Dr.getOrdinal("Field2")) 
      __Rtn.Add(__Data) 
    End While

    Return __Rtn

  Finally 
    If Not(__Dr is Nothing) AndAlso __Dr.isClosed() = false then __Dr.Close 
  End Try

End Class

So in this case I would have a class called MyData

Class MyData
  Protected _Field1 as Integer
  Public Property Field1 as Integer
    Get
       Return _Field1
    End Get

    Set (Value As Integer)
       _Field1 = Value
    End Set
  End Property

  Protected _Field2 as String
  Public Property Field2 as String
    Get
       Return _Field2
    End Get

    Set (Value As Integer)
       _Field1 = Value
    End Set
  End Property
End Class

and a collection class called CollectionOfMyData (which is way more complicated than I really want to document here... suffice to say start with a CollectionBase (or if Rob would publish his Collections there would be something even better). 

I really like this method because I can easily pass around the individual row all over the place (in session, viewstate as well as in a list) and the resulting object is fairly light weight.  When it comes to sorting (which was my problem before), I use Rob Teixeira's collection classes that he built here at work (I really wish he would release them); they have some built in methods for easily getting sorting/filtering to work.

The other thing you may notice is that I have separated my data access from the main part of the program (I actually have these classes in another project all together)... it forces me to do more work if a need to add a new field, but more than likely I would have to do something to display the field anyway.  The other thing is that if there is a problem with the communication with the database, I know exactly where in the code the problem is.

Print | posted on Friday, May 28, 2004 10:26 AM

Feedback

# re: DataSet discussion revisited...

left by at 5/28/2004 7:05 AM Gravatar

I've been working with ASP .NET for about 2 years now and I have also abandoned the dataset and adopted a strategy similar to yours.  I do think the dataset has uses for RAD in winforms though being it automatically does updates etc.  It's fairly useless for me though since for some reason I'm obsessed with putting all my database code into stored procs.

<br>

<br>****** My usual structure ******

<br>

<br>Stored Procedures

<br>MS - Data access application block

<br>C# Project for classes representing my &quot;business&quot; objects

<br>ASP .NET Project

<br>

<br>****************************

<br>

<br>The nice thing about this structure is that should I decide to build a winforms application I can just reference the object project and at that point it's just designing the gui and gluing the business objects to the forms.

<br>

# RE: DataSet discussion revisited...

left by at 5/28/2004 7:10 AM Gravatar

A more robust way of getting data from a data source is to use XPathNavigators.  Because an XPathNavigator can be used to tranverse any store that implements the IXPathNavigable interface, it can work on XML, Datasets, or any other custom data store.  See <a target="_new" href="msdn.microsoft.com/.../default.asp .  This is one of the best practices topics that the XML MVPs are working on getting out to the public (soon I promise :) ).

# re: DataSet discussion revisited...

left by at 5/28/2004 7:14 AM Gravatar

Eric,

<br>

<br>Glad to see that someone else approves...

<br>

<br>DonXML!  (What did I do to deserve the honor!) I'm clicking on the link now....

# re: DataSet discussion revisited...

left by at 5/30/2004 9:13 AM Gravatar

Being a VB code junkie, I couldn't resist :-)

<br>

<br>The code posted could be made a bit more efficient with two small improvements. The first one:

<br>

<br>__dr = sqlHelper.ExecuteDataReader(…)

<br>Try

<br>  …

<br>Finally

<br>  __dr.Close

<br>End Try

<br>

<br>That is, instantiate the data reader *before* the Try statement and you don't have to worry checking it for IsClosed and Nothing in the Finally block. (This makes the syntax a little bit closer to the compact C# using statement syntax)

<br>

<br>The second one:

<br>

<br>Dim Field1Index As Integer = __dr.getOrdinal(&quot;Field1&quot;)

<br>Dim Field2Index As Integer = __dr.getOrdinal(&quot;Field2&quot;)

<br>While __dr.read()

<br>  Dim __Data as MyData = New MyData

<br>  __Data.Field1 = __Dr.GetInt32(Field1Index)

<br>  __Data.Field2 = __Dr.GetString(Field2Index)

<br>  __Rtn.Add(__Data)

<br>End While

<br>

<br>That is, get the field indexes *before* the loop incurring the field name lookup overhead only once. I did some simple tests and I've observed performance improvements from 32 up to 60% in favor for the cached field indexes.

<br>

<br>Just my 2 cents…

# re: DataSet discussion revisited...

left by at 5/31/2004 6:22 PM Gravatar

The last one I definitely agree with... I need to maybe start using that, but DonXML actually showed me some new tricks... I need to play with the tricks first before I post (thereby giving him a little bit of time to post about it...

<br>

<br>I'm not sure about the second one... the whole purpose of the data reader being executed inside the try... catch is so that I can catch any exceptions (it could be a coding issue... it could be the SQL server's HD crashing... or something in between)...

<br>

<br>Ultimately the bottom line is that I have divorced myself from the dataset once and for all (I'd rather have the power and control)... ok at least I've divorced myself from it for most situations...

<br>

<br>Just my 2 cents...

# Blog Clusters

left by at 6/3/2004 8:24 AM Gravatar

Blog Clusters

# Performance Statistics of Various Implementations of the Data Mapper Pattern

left by at 6/28/2004 6:07 AM Gravatar
Title  
Name
Email (never displayed)
Url
Comments   
Please add 5 and 4 and type the answer here: