same code, different providers => different behaviours??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bart

    #1

    same code, different providers => different behaviours??

    Hi,

    I ran the same code with two different providers (oledb abd sqlclient), and
    i got two different behaviours.
    The code with OLEDB runs perfect without error.
    The same code with SQLClient gives an error at line: "dtreader =
    comd.ExecuteRea der" (the second)
    "There is already an open DataReader associated with this Command which must
    be closed first."

    Why must the DataReader be closed with provider SqlClient and not with
    Oledb?
    Any explanation for that?

    Thanks
    Bart

    1) with OleDb:
    -------------
    Imports System.Data.Ole Db
    Partial Class studalres
    Inherits System.Web.UI.P age
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    Dim oConnection As OleDbConnection
    Dim comd As OleDbCommand
    Dim dtreader As OleDbDataReader
    Dim sql As String

    oConnection = New OleDbConnection ()
    oConnection.Con nectionString =
    System.Configur ation.Configura tionManager.Con nectionStrings( "oledbdemo").To String()
    oConnection.Ope n()

    sql = "SELECT pc.naam FROM pc ;"
    comd = New OleDbCommand(sq l, oConnection)
    dtreader = comd.ExecuteRea der
    'dtreader.Close ()

    sql = "SELECT pc.naam FROM pc ;"
    comd = New OleDbCommand(sq l, oConnection)
    dtreader = comd.ExecuteRea der
    dtreader.Close( )
    oConnection.Clo se()
    End Sub
    End Class

    2) with SqlClient :
    ----------------
    Imports System.Data
    Imports System.Data.sql client
    Partial Class studalres
    Inherits System.Web.UI.P age
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    Dim oConnection As SqlConnection
    Dim comd As SqlCommand
    Dim dtreader As SqlDataReader
    Dim sql As String

    oConnection = New SqlConnection()
    oConnection.Con nectionString =
    System.Configur ation.Configura tionManager.Con nectionStrings( "sqlclientdemo" ).ToString()
    oConnection.Ope n()

    sql = "SELECT pc.naam FROM pc ;"
    comd = New SqlCommand(sql, oConnection)
    dtreader = comd.ExecuteRea der
    'dtreader.Close ()

    sql = "SELECT pc.naam FROM pc ;"
    comd = New SqlCommand(sql, oConnection)
    dtreader = comd.ExecuteRea der
    'here is the error
    dtreader.Close( )
    oConnection.Clo se()
    End Sub
    End Class



  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: same code, different providers => different behaviours??

    Each reader needs a connection, and the provider may either open a new
    connection in the background or refuse to run the query. This can be
    controlled from a setting in the connection string, and the default
    value of this setting is obviously different for these providers.

    Bart wrote:
    Hi,
    >
    I ran the same code with two different providers (oledb abd sqlclient), and
    i got two different behaviours.
    The code with OLEDB runs perfect without error.
    The same code with SQLClient gives an error at line: "dtreader =
    comd.ExecuteRea der" (the second)
    "There is already an open DataReader associated with this Command which must
    be closed first."
    >
    Why must the DataReader be closed with provider SqlClient and not with
    Oledb?
    Any explanation for that?
    >
    Thanks
    Bart
    >
    1) with OleDb:
    -------------
    Imports System.Data.Ole Db
    Partial Class studalres
    Inherits System.Web.UI.P age
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    Dim oConnection As OleDbConnection
    Dim comd As OleDbCommand
    Dim dtreader As OleDbDataReader
    Dim sql As String
    >
    oConnection = New OleDbConnection ()
    oConnection.Con nectionString =
    System.Configur ation.Configura tionManager.Con nectionStrings( "oledbdemo").To String()
    oConnection.Ope n()
    >
    sql = "SELECT pc.naam FROM pc ;"
    comd = New OleDbCommand(sq l, oConnection)
    dtreader = comd.ExecuteRea der
    'dtreader.Close ()
    >
    sql = "SELECT pc.naam FROM pc ;"
    comd = New OleDbCommand(sq l, oConnection)
    dtreader = comd.ExecuteRea der
    dtreader.Close( )
    oConnection.Clo se()
    End Sub
    End Class
    >
    2) with SqlClient :
    ----------------
    Imports System.Data
    Imports System.Data.sql client
    Partial Class studalres
    Inherits System.Web.UI.P age
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    Dim oConnection As SqlConnection
    Dim comd As SqlCommand
    Dim dtreader As SqlDataReader
    Dim sql As String
    >
    oConnection = New SqlConnection()
    oConnection.Con nectionString =
    System.Configur ation.Configura tionManager.Con nectionStrings( "sqlclientdemo" ).ToString()
    oConnection.Ope n()
    >
    sql = "SELECT pc.naam FROM pc ;"
    comd = New SqlCommand(sql, oConnection)
    dtreader = comd.ExecuteRea der
    'dtreader.Close ()
    >
    sql = "SELECT pc.naam FROM pc ;"
    comd = New SqlCommand(sql, oConnection)
    dtreader = comd.ExecuteRea der
    'here is the error
    dtreader.Close( )
    oConnection.Clo se()
    End Sub
    End Class
    >
    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Bart

      #3
      Re: same code, different providers => different behaviours??

      Thanks

      "Göran Andersson" <guffa@guffa.co mschreef in bericht
      news:OUayAsFbHH A.2300@TK2MSFTN GP06.phx.gbl...
      Each reader needs a connection, and the provider may either open a new
      connection in the background or refuse to run the query. This can be
      controlled from a setting in the connection string, and the default value
      of this setting is obviously different for these providers.
      >
      Bart wrote:
      >Hi,
      >>
      >I ran the same code with two different providers (oledb abd sqlclient),
      >and i got two different behaviours.
      >The code with OLEDB runs perfect without error.
      >The same code with SQLClient gives an error at line: "dtreader =
      >comd.ExecuteRe ader" (the second)
      >"There is already an open DataReader associated with this Command which
      >must be closed first."
      >>
      >Why must the DataReader be closed with provider SqlClient and not with
      >Oledb?
      >Any explanation for that?
      >>
      >Thanks
      >Bart
      >>
      >1) with OleDb:
      >-------------
      >Imports System.Data.Ole Db
      >Partial Class studalres
      > Inherits System.Web.UI.P age
      > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
      >System.EventAr gs) Handles Me.Load
      > Dim oConnection As OleDbConnection
      > Dim comd As OleDbCommand
      > Dim dtreader As OleDbDataReader
      > Dim sql As String
      >>
      > oConnection = New OleDbConnection ()
      > oConnection.Con nectionString =
      >System.Configu ration.Configur ationManager.Co nnectionStrings ("oledbdemo").T oString()
      > oConnection.Ope n()
      >>
      > sql = "SELECT pc.naam FROM pc ;"
      > comd = New OleDbCommand(sq l, oConnection)
      > dtreader = comd.ExecuteRea der
      > 'dtreader.Close ()
      >>
      > sql = "SELECT pc.naam FROM pc ;"
      > comd = New OleDbCommand(sq l, oConnection)
      > dtreader = comd.ExecuteRea der
      > dtreader.Close( )
      > oConnection.Clo se()
      > End Sub
      >End Class
      >>
      >2) with SqlClient :
      >----------------
      >Imports System.Data
      >Imports System.Data.sql client
      >Partial Class studalres
      > Inherits System.Web.UI.P age
      > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
      >System.EventAr gs) Handles Me.Load
      > Dim oConnection As SqlConnection
      > Dim comd As SqlCommand
      > Dim dtreader As SqlDataReader
      > Dim sql As String
      >>
      > oConnection = New SqlConnection()
      > oConnection.Con nectionString =
      >System.Configu ration.Configur ationManager.Co nnectionStrings ("sqlclientdemo ").ToString ()
      > oConnection.Ope n()
      >>
      > sql = "SELECT pc.naam FROM pc ;"
      > comd = New SqlCommand(sql, oConnection)
      > dtreader = comd.ExecuteRea der
      > 'dtreader.Close ()
      >>
      > sql = "SELECT pc.naam FROM pc ;"
      > comd = New SqlCommand(sql, oConnection)
      > dtreader = comd.ExecuteRea der 'here is the error
      > dtreader.Close( )
      > oConnection.Clo se()
      > End Sub
      >End Class
      >>
      >
      --
      Göran Andersson
      _____
      http://www.guffa.com

      Comment

      Working...