Results 1 to 7 of 7

Thread: Select certain rows into dataset

  1. #1

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306

    Select certain rows into dataset

    Hi All,

    This may belong in the database section, not sure. Anyway, I am selecting approx 1000 rows in a storedprocedure into a dataset using a dataadapter (sql Server 2k)

    I know I can say "SELECT TOP 100 from etc" but what I wanna do is select records 100 to 200.

    Is there a way to do this with sql/dataadapters? or are you gonna tell me to select the top 200 then advance 100?

    I just didnt wanna waste me memory... unless anyone thinks selecting 1000 records will make hardly any difference...

    thanks for any help

    GaZ

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    To do it with SQL Server:

    SELECT TOP 100 * FROM ( SELECT TOP 100 * FROM ( SELECT TOP 200 * FROM [TableName] ORDER BY IDENTITYCOL ASC ) ORDER BY IDENTITYCOL DESC ) ORDER BY IDENTITYCOL ASC

    The best way, though, is to assign the results to a datatable, assign the datatable to a dataview and then use a filter on the dataview to see just the records you are looking for.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    As for wasting memory:

    If you are not using fields of the types ntext, text, image or binary, then don't worry about it. That's why I suggested the datatable/dataview combo. Using that set up, you don't have to requery if you decide to view a different subset of records.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  4. #4

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306
    Hi, thanks for replying!

    The best way, though, is to assign the results to a datatable, assign the datatable to a dataview and then use a filter on the dataview to see just the records you are looking for.
    Could you explain that a bit more please? No rush, I'm knackered and calling it a day now hehe

    Point behind this, is that I don't wanna select 1000 records when only 100 are needed... seems a waste :] I'm not using any of the uber-big field types tho in this select so I might just stick with it.

    Again, thanks for replying... didn't expect anything till 2morrow

    GaZ

  5. #5
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Consider this function that I use with my applications:

    PHP Code:
            Public Shared Function ReturnMultipleResults(ByVal sSQLText As StringOptional ByRef objTrace As Object Nothing) As DataTable
                Dim objConn 
    As New SqlClient.SqlConnection(g_ConnString)
                
    Dim objCmd As New SqlClient.SqlCommand(sSQLTextobjConn)
                
    Dim objRS As SqlClient.SqlDataReader
                objConn
    .Open()
                
    Dim objDT As New DataTable("success")
                Try
                    
    objRS objCmd.ExecuteReader
                
    Catch ee As Exception
                    objConn
    .Close()
                    
    objDT.TableName "nothing"
                    
    If Not objTrace Is Nothing Then
                        objTrace
    .Warn("SQL Failed: " ee.Message)
                        
    objTrace.warn(objCmd.CommandText)
                    
    End If
                    Return 
    objDT
                End 
    Try

                If 
    Not objRS.Read() Then
                    objDT
    .TableName "nothing"
                    
    If Not objTrace Is Nothing Then
                        objTrace
    .warn("Empty Result Set: " objCmd.CommandText)
                    
    End If
                    
    objRS.Close()
                    
    objConn.Close()
                    Return 
    objDT
                End 
    If

                
    Dim iX As Integer
                
    For iX 0 To objRS.FieldCount 1
                    objDT
    .Columns.Add(objRS.GetName(iX))
                
    Next

                
    Do
                    
    Dim objRow As DataRow
                    objRow 
    objDT.NewRow
                    
    For iX 0 To objRS.FieldCount 1
                        objRow
    .Item(iX) = objRS.Item(iX)
                    
    Next
                    objDT
    .Rows.Add(objRow)
                
    Loop While objRS.Read()
                
    objRS.Close()
                
    objConn.Close()
                Return 
    objDT
            End 
    Function 
    It returns a datatable from whatever sql string I supply.

    Once I get that datatable, I create a dataview object and set the first table to that datatable.

    Finally, I can filter the dataview for any 100 records I want.

    Futher, if I have to manipulate the data at all anywhere down the line, I can refilter without a hit back to the server which, in turn, will increase the performace of this application.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Why not use a DataAdapter to do the dirty work:
    VB Code:
    1. Public Shared Function ReturnMultipleResultsEasy(ByVal cmdText As String) As DataTable
    2.     Dim cn As New SqlConnection(g_ConnString)
    3.     Dim cmd As New SqlCommand(cmdText, cn)
    4.     Dim da As New SqlDataAdapter(cmd)
    5.     Dim dt As New DataTable
    6.     da.Fill(dt)
    7.     Return dt
    8. End Function
    ...less work, easier to read, although the names of these functions are missleading, neither can really return multiple results as only one datatable is ever returned. The dataview approach to paging is nice and easy but could lead to stale data. There are TONS of articles on the web for paging resultsets, stored proc techniques and others that are pretty cool, which one to use just depends on the situation and how much work you're willing to do, just hit google for paging resultsets.

  7. #7

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306

    Thumbs up

    thanks lord_rat and pvb

    GaZ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width