Results 1 to 5 of 5

Thread: Thoughts on ADO.Net DataReader

  1. #1

    Thread Starter
    Hyperactive Member stingrae's Avatar
    Join Date
    Apr 2002
    Location
    Sydney
    Posts
    401

    Thoughts on ADO.Net DataReader

    I was wondering what your thoughts were on the ADO.Net DateReader.

    One thing I HATE is that you cannot have a DataReader open, and yet at the same time perform other operations on the database.

    For example, if you've got a data areader open and whilst open you attempt an insert on a (completely different) table, you get the following error:

    Additional information: There is already an open DataReader associated with this Connection which must be closed first.
    you could do this in ADO 2.x

    tsk tsk tsk
    "The passion lives to keep your faith, though all are different, all are great" ... Michael Hutchence 1960-1997.

    Windows & Web Developer
    Specialising in Visual Basic .Net & Client Server Programming & Client/Customer Relations Databases
    Sutherland Shire, Sydney Australia
    www.stingrae.com.au
    Developer of Arnold - Gym & Martial Arts Database Management System
    www.gymdatabase.com.au

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Usually it seems that you wouldn't need to do both at the sametime, but if you do you just have to use a different connection object. The Datareader locks the connection object, since its a forward-only, read-only reader, but you should be able to do other things to the database as long as you aren't using the same connection object.

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    What's the big problem if you closed datareader connection after you're done . .

  4. #4

    Thread Starter
    Hyperactive Member stingrae's Avatar
    Join Date
    Apr 2002
    Location
    Sydney
    Posts
    401
    guys, the problem is this. say you have a datareader going through a group of records, and depending upon the values in one (or more) of the fields, you wish to run a simple update query to set a flag on another table. something like:

    Code:
    do while DataReader.Read  = True
       If DataReader(Field) = ValueX then
           Run CommandX
       End If
    loop
    DataReader.Close
    this will fail at the Run Command Line. The way I get around this is by storing values into an array, and then running all the commands. It is annoying though, because the progress bars and other things don't look so smooth, plus it is a bit of extra coding and to top it off, is a pain in the proverbial!

    just my $0.02!
    "The passion lives to keep your faith, though all are different, all are great" ... Michael Hutchence 1960-1997.

    Windows & Web Developer
    Specialising in Visual Basic .Net & Client Server Programming & Client/Customer Relations Databases
    Sutherland Shire, Sydney Australia
    www.stingrae.com.au
    Developer of Arnold - Gym & Martial Arts Database Management System
    www.gymdatabase.com.au

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    You're gonna lose a big benefit of the datareader if you're doing operations while looping through the resultset(because you're holding that connection open and wasting resources) so why not load up a dataset, and spare your connection for use on your updates or for other clients who need a connection?

    for example, i have a State table and a State2 table, both identical in structure:
    Code:
    Create Table State
    (
    		StateCode char ( 2 ) not null
    	,	StateName varchar ( 50 ) not null
    )
    The State table has, you guessed it, a list of the states. The second one(State2) is empty. So the following just loops thru and inserts each row in the State table into the State2 table:
    VB Code:
    1. Imports System.Data
    2. Imports System.Data.SqlClient
    3.  
    4. Module Module1
    5.  
    6.     Sub Main()
    7.         Dim connString As String = "user id=sa;password=sa;database=scratch;server=DeathAngel;"
    8.         Dim cn As New SqlConnection(connString)
    9.         Dim ds As New DataSet("States")
    10.         Dim cmdText As String = "Select StateCode, StateName From State"
    11.         Dim cmd As New SqlCommand(cmdText, cn)
    12.         Dim da As New SqlDataAdapter(cmd)
    13.         Try
    14.             cn.Open()
    15.             da.Fill(ds)
    16.             For Each dataRow As DataRow In ds.Tables(0).Rows
    17.                 Add(dataRow("StateCode").ToString(), dataRow("StateName").ToString(), cn)
    18.             Next
    19.         Finally
    20.             If Not da Is Nothing Then
    21.                 da.Dispose()
    22.             End If
    23.             If Not cmd Is Nothing Then
    24.                 cmd.Dispose()
    25.             End If
    26.             If Not cn Is Nothing Then
    27.                 cn.Close()
    28.                 cn.Dispose()
    29.             End If
    30.         End Try
    31.         Console.WriteLine("Done...")
    32.         Console.ReadLine()
    33.     End Sub
    34.  
    35.     Sub Add(ByVal stateCode As String, ByVal stateName As String, ByRef cn As SqlConnection)
    36.         Dim cmdText As String = "Insert Into State2 ( StateCode , StateName ) Values " & _
    37.             "( @StateCode , @StateName )"
    38.         Dim cmd As New SqlCommand(cmdText, cn)
    39.         cmd.Parameters.Add(New SqlParameter("@StateCode", stateCode))
    40.         cmd.Parameters.Add(New SqlParameter("@StateName", stateName))
    41.         cmd.ExecuteNonQuery()
    42.     End Sub
    43. End Module
    The resultset here is only 51 rows, but what if your resultset was say 1000 rows. That means you're holding the connection open and locked for no one else(including yourself) to use.

    Another solution if using the datareader is that important is to just open another connection for the updates.

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