using statment with sql connection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John J. Hughes II

    using statment with sql connection

    I was wondering about the follow code. I know that dispose is called on an
    object when it leaves the using clause but was also wondering if the closed
    statement was called on objects like Sql Connection and Data Readers?
    Basically will I have problem or leave an object in memory if the closed is
    not called below?

    using(SqlConnec tion con = new SqlConnection() )
    {
    con.Open();
    using(SqlComman d cmd = con.CreateComma nd)
    {
    ... set up
    using(SqlDataRe ader dr = cmd.ExecuteDate Reader())
    {
    while(dr.Read() )
    ..... do something
    }
    }
    }

    Regards,
    John


  • Cordell Lawrence

    #2
    Re: using statment with sql connection

    John, in both cases the object's Close( ) methods are called in their
    respective Dispose( ) methods. In a situation where you don't wrap your code
    in using statements, then I suppose not calling the Close methods of these
    objects only become a problem if they are long lived or objects scoped as
    members of some class that is long lived, otherwise when the objects fall
    out of scope they should be disposed of.

    Hope this helps.
    Cordell Lawrence
    Teleios Systems Ltd.

    "John J. Hughes II" <no@invalid.com > wrote in message
    news:eJSXT%23rP FHA.2604@TK2MSF TNGP10.phx.gbl. ..[color=blue]
    > I was wondering about the follow code. I know that dispose is called on[/color]
    an[color=blue]
    > object when it leaves the using clause but was also wondering if the[/color]
    closed[color=blue]
    > statement was called on objects like Sql Connection and Data Readers?
    > Basically will I have problem or leave an object in memory if the closed[/color]
    is[color=blue]
    > not called below?
    >
    > using(SqlConnec tion con = new SqlConnection() )
    > {
    > con.Open();
    > using(SqlComman d cmd = con.CreateComma nd)
    > {
    > ... set up
    > using(SqlDataRe ader dr = cmd.ExecuteDate Reader())
    > {
    > while(dr.Read() )
    > ..... do something
    > }
    > }
    > }
    >
    > Regards,
    > John
    >
    >[/color]


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: using statment with sql connection

      Hi,

      The close operation will be performed when the Dispose event, now as this
      is not deterministic you don't have control of when will that happen,
      therefore it's advised that you close your connectin as soon as you finish
      with it.

      cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation


      "John J. Hughes II" <no@invalid.com > wrote in message
      news:eJSXT%23rP FHA.2604@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      >I was wondering about the follow code. I know that dispose is called on an
      >object when it leaves the using clause but was also wondering if the closed
      >statement was called on objects like Sql Connection and Data Readers?
      >Basically will I have problem or leave an object in memory if the closed is
      >not called below?
      >
      > using(SqlConnec tion con = new SqlConnection() )
      > {
      > con.Open();
      > using(SqlComman d cmd = con.CreateComma nd)
      > {
      > ... set up
      > using(SqlDataRe ader dr = cmd.ExecuteDate Reader())
      > {
      > while(dr.Read() )
      > ..... do something
      > }
      > }
      > }
      >
      > Regards,
      > John
      >[/color]


      Comment

      • Tom Porterfield

        #4
        Re: using statment with sql connection

        Ignacio Machin ( .NET/ C# MVP ) wrote:[color=blue]
        > Hi,
        >
        > The close operation will be performed when the Dispose event, now as this
        > is not deterministic you don't have control of when will that happen,
        > therefore it's advised that you close your connectin as soon as you finish
        > with it.[/color]

        ?? I thought Disposed was deterministic, only finalize is not
        deterministic. In his example Dispose should be called when the using block
        is exited.
        --
        Tom Porterfield


        Comment

        • John J. Hughes II

          #5
          Re: using statment with sql connection

          Thanks,
          John

          "Cordell Lawrence" <clawrence@tele ios-systems.com> wrote in message
          news:eD9EsGsPFH A.1500@TK2MSFTN GP09.phx.gbl...[color=blue]
          > John, in both cases the object's Close( ) methods are called in their
          > respective Dispose( ) methods. In a situation where you don't wrap your
          > code
          > in using statements, then I suppose not calling the Close methods of these
          > objects only become a problem if they are long lived or objects scoped as
          > members of some class that is long lived, otherwise when the objects fall
          > out of scope they should be disposed of.
          >
          > Hope this helps.
          > Cordell Lawrence
          > Teleios Systems Ltd.
          >
          > "John J. Hughes II" <no@invalid.com > wrote in message
          > news:eJSXT%23rP FHA.2604@TK2MSF TNGP10.phx.gbl. ..[color=green]
          >> I was wondering about the follow code. I know that dispose is called on[/color]
          > an[color=green]
          >> object when it leaves the using clause but was also wondering if the[/color]
          > closed[color=green]
          >> statement was called on objects like Sql Connection and Data Readers?
          >> Basically will I have problem or leave an object in memory if the closed[/color]
          > is[color=green]
          >> not called below?
          >>
          >> using(SqlConnec tion con = new SqlConnection() )
          >> {
          >> con.Open();
          >> using(SqlComman d cmd = con.CreateComma nd)
          >> {
          >> ... set up
          >> using(SqlDataRe ader dr = cmd.ExecuteDate Reader())
          >> {
          >> while(dr.Read() )
          >> ..... do something
          >> }
          >> }
          >> }
          >>
          >> Regards,
          >> John
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • John J. Hughes II

            #6
            Re: using statment with sql connection

            Ok, I had a feeling about that. Basically I could end up with a bunch of
            open connection waiting for the system to get around to closing them which
            could be a problem.

            Thanks,
            John

            "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
            in message news:%23lIAwOsP FHA.880@TK2MSFT NGP15.phx.gbl.. .[color=blue]
            > Hi,
            >
            > The close operation will be performed when the Dispose event, now as this
            > is not deterministic you don't have control of when will that happen,
            > therefore it's advised that you close your connectin as soon as you finish
            > with it.
            >
            > cheers,
            >
            > --
            > Ignacio Machin,
            > ignacio.machin AT dot.state.fl.us
            > Florida Department Of Transportation
            >
            >
            > "John J. Hughes II" <no@invalid.com > wrote in message
            > news:eJSXT%23rP FHA.2604@TK2MSF TNGP10.phx.gbl. ..[color=green]
            >>I was wondering about the follow code. I know that dispose is called on
            >>an object when it leaves the using clause but was also wondering if the
            >>closed statement was called on objects like Sql Connection and Data
            >>Readers? Basically will I have problem or leave an object in memory if the
            >>closed is not called below?
            >>
            >> using(SqlConnec tion con = new SqlConnection() )
            >> {
            >> con.Open();
            >> using(SqlComman d cmd = con.CreateComma nd)
            >> {
            >> ... set up
            >> using(SqlDataRe ader dr = cmd.ExecuteDate Reader())
            >> {
            >> while(dr.Read() )
            >> ..... do something
            >> }
            >> }
            >> }
            >>
            >> Regards,
            >> John
            >>[/color]
            >
            >[/color]


            Comment

            • Ignacio Machin \( .NET/ C# MVP \)

              #7
              Re: using statment with sql connection

              Hi Tom,

              My mistake, you are 100% right, Dispose is deterministic

              sorry for the error, going to get some expresso in my bloodstream :)

              cheers,

              --
              Ignacio Machin,
              ignacio.machin AT dot.state.fl.us
              Florida Department Of Transportation


              "Tom Porterfield" <tpporter@mvps. org> wrote in message
              news:%23gDLiYsP FHA.1388@TK2MSF TNGP09.phx.gbl. ..[color=blue]
              > Ignacio Machin ( .NET/ C# MVP ) wrote:[color=green]
              >> Hi,
              >>
              >> The close operation will be performed when the Dispose event, now as
              >> this
              >> is not deterministic you don't have control of when will that happen,
              >> therefore it's advised that you close your connectin as soon as you
              >> finish
              >> with it.[/color]
              >
              > ?? I thought Disposed was deterministic, only finalize is not
              > deterministic. In his example Dispose should be called when the using
              > block
              > is exited.
              > --
              > Tom Porterfield
              >
              >[/color]


              Comment

              • John J. Hughes II

                #8
                Re: using statment with sql connection

                So basically if I can stop writing close on these short read commands,
                that's nice, saves a lot of finally statments in my code.

                Regards,
                John


                Comment

                Working...