Re: Limit Rows in DataTable

Tony Bermudez <[email protected]> Tue, 29 Apr 2008 19:41:20 -0500
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <005a01c8aa5a$e9345a40$bb9d0ec0$@net>
Is there any way that you can post the C# equivalent code?

Thanks,
Tony

-----Original Message-----
From: Discussion of development on the .NET platform using any managed =
language [mailto:[email protected]] On Behalf Of Marc =
Brooks
Sent: Tuesday, April 29, 2008 12:01 PM
To: [email protected]
Subject: Re: [DOTNET-CLR] Limit Rows in DataTable

> Be careful with this.  We recently had a customer do an emailing with =
22,000
> rows in a datatable in a dataset.  The dataset simply is not designed =
for
> this amount of records and it was SLOW. We broke it up into smaller =
datasets
> of about 500 record each.

I'll second this, and remind people that  <singing> Anything you can
do with a DataSet, I can do with a DataReader faster </singing>

My usual pattern these days is to pass a delegate that takes the
SqlDataReader and new up the DTO and parties on... then call that guy
inside the loop body in my DAL.  (which prevents Dispose-forgotten
leaks). Something like this in VB for variety :):

    Public Interface ILoadFromReader
        Sub LoadFromReader(ByVal reader As SqlDataReader)
    End Interface

        Public Function ExecuteDataReader(Of T As {New,
ILoadFromReader})(ByVal connection As SqlConnection, ByVal commandType
As CommandType, ByVal commandText As String, ByVal handler As
HandleOne(Of T), ByVal ParamArray commandParameters() As
SqlClient.SqlParameter) As Long

            If (connection Is Nothing) Then Throw New
ArgumentNullException("connection")
            ' Create a command and prepare it for execution
            Dim cmd As SqlCommand =3D New SqlCommand
            Dim mustCloseConnection As Boolean =3D False

            Try
                PrepareCommand(cmd, connection, CType(Nothing,
SqlTransaction), commandType, commandText, commandParameters,
mustCloseConnection)

                Dim count As Long =3D 0
                Dim reader As SqlDataReader =3D Nothing
                Try
                    reader =3D cmd.ExecuteReader()

                    If reader.HasRows Then

                        While reader.Read()
                            count =3D count + 1
                            Dim entry As T =3D New T()
                            entry.LoadFromReader(reader)
                            If Not handler(entry) Then Exit While
                        End While
                    End If
                Finally
                    If Not (reader Is Nothing) Then reader.Dispose()
                End Try

                ' Detach the SqlParameters from the command object, so
they can be used again
                cmd.Parameters.Clear()

                Return count
            Finally
                If (mustCloseConnection) Then connection.Close()
            End Try
        End Function

--
"Your lack of planning DOES constitute an emergency on my part... so
PLAN BETTER! "

Marc C. Brooks
http://musingmarc.blogspot.com

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
This list is hosted by DevelopMentor=EF=BF=BD  http://www.develop.com

View archives and manage your subscription(s) at =
http://discuss.develop.com

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
This list is hosted by DevelopMentor=AE  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com