is it a good idea to use SQL connection?

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

    is it a good idea to use SQL connection?

    Hi,

    (newbie in C#) is it a good idea to use SQL Connection, SQL Data Adapter,...
    in C#? when I am design a form, I can use SQL Connection and set the
    properties to connect to a database. I guess after unloading the form, this
    connection will be closed. but as I know, it's better to open a connection
    when we need it and after using, we should close it. I can do it without
    using SQL Connection component (by using SqlConnection class). now which one
    is better? using SQL Connection is very easy but I'd like to have the best
    performance.

    thanks,
    Mojtaba


  • Miha Markic [MVP C#]

    #2
    Re: is it a good idea to use SQL connection?

    Hi Mojtaba,

    In designer you only design the connection - it won't open it for you thus
    it won't make much difference.
    Though I preffer to create it by code right before I need it and dispose it
    as soon as possible.

    --
    Miha Markic [MVP C#] - RightHand .NET consulting & development
    miha at rthand com


    "Mojtaba Faridzad" <mfaridzad@hotm ail.com> wrote in message
    news:Oc$hnz1jEH A.3712@TK2MSFTN GP15.phx.gbl...[color=blue]
    > Hi,
    >
    > (newbie in C#) is it a good idea to use SQL Connection, SQL Data
    > Adapter,... in C#? when I am design a form, I can use SQL Connection and
    > set the properties to connect to a database. I guess after unloading the
    > form, this connection will be closed. but as I know, it's better to open a
    > connection when we need it and after using, we should close it. I can do
    > it without using SQL Connection component (by using SqlConnection class).
    > now which one is better? using SQL Connection is very easy but I'd like to
    > have the best performance.
    >
    > thanks,
    > Mojtaba
    >[/color]


    Comment

    • Mark Broadbent

      #3
      Re: is it a good idea to use SQL connection?

      also bear in mind Mojtaba that the SqlAdapter will dynamically open and
      close the connection for you, so you should always let it do so (as opposed
      to manually opening and closing the connection).

      "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
      news:OtI54%231j EHA.1644@tk2msf tngp13.phx.gbl. ..[color=blue]
      > Hi Mojtaba,
      >
      > In designer you only design the connection - it won't open it for you thus
      > it won't make much difference.
      > Though I preffer to create it by code right before I need it and dispose[/color]
      it[color=blue]
      > as soon as possible.
      >
      > --
      > Miha Markic [MVP C#] - RightHand .NET consulting & development
      > miha at rthand com
      > www.rthand.com
      >
      > "Mojtaba Faridzad" <mfaridzad@hotm ail.com> wrote in message
      > news:Oc$hnz1jEH A.3712@TK2MSFTN GP15.phx.gbl...[color=green]
      > > Hi,
      > >
      > > (newbie in C#) is it a good idea to use SQL Connection, SQL Data
      > > Adapter,... in C#? when I am design a form, I can use SQL Connection and
      > > set the properties to connect to a database. I guess after unloading the
      > > form, this connection will be closed. but as I know, it's better to open[/color][/color]
      a[color=blue][color=green]
      > > connection when we need it and after using, we should close it. I can do
      > > it without using SQL Connection component (by using SqlConnection[/color][/color]
      class).[color=blue][color=green]
      > > now which one is better? using SQL Connection is very easy but I'd like[/color][/color]
      to[color=blue][color=green]
      > > have the best performance.
      > >
      > > thanks,
      > > Mojtaba
      > >[/color]
      >
      >[/color]


      Comment

      • Miha Markic [MVP C#]

        #4
        Re: is it a good idea to use SQL connection?

        Hi Mark,

        "Mark Broadbent" <no-spam-please@no-spam-please.com> wrote in message
        news:e2$31J2jEH A.1644@tk2msftn gp13.phx.gbl...[color=blue]
        > also bear in mind Mojtaba that the SqlAdapter will dynamically open and
        > close the connection for you, so you should always let it do so (as
        > opposed
        > to manually opening and closing the connection).[/color]

        I tend to disagree.
        You should manually open the connections if you have more than one operation
        to do within the same connection.
        Not to speak about Update when it is supposed to be a part of transaction
        :-)

        --
        Miha Markic [MVP C#] - RightHand .NET consulting & development
        miha at rthand com



        Comment

        • Mojtaba Faridzad

          #5
          Re: is it a good idea to use SQL connection?

          thanks Mark!

          so a combination of SqlConnection, SqlDataAdapter, SqlCommand, and DataSet
          (by drag and drop it from toolbox) has the same performance as open them in
          source code? in source code, I use a "try" block to open the connection and
          fill the DataAdapter and close the connection.

          the reason of asking this question is that I am reading "Prof. C# 2nd
          Edition" which teaches programming method of connection to a database. but I
          downloaded a sample program which opens and works with data just by toolbox
          components. I confused a little bit which method it's better.

          thanks again

          "Mark Broadbent" <no-spam-please@no-spam-please.com> wrote in message
          news:e2$31J2jEH A.1644@tk2msftn gp13.phx.gbl...[color=blue]
          > also bear in mind Mojtaba that the SqlAdapter will dynamically open and
          > close the connection for you, so you should always let it do so (as
          > opposed
          > to manually opening and closing the connection).
          >
          > "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
          > news:OtI54%231j EHA.1644@tk2msf tngp13.phx.gbl. ..[color=green]
          >> Hi Mojtaba,
          >>
          >> In designer you only design the connection - it won't open it for you
          >> thus
          >> it won't make much difference.
          >> Though I preffer to create it by code right before I need it and dispose[/color]
          > it[color=green]
          >> as soon as possible.
          >>
          >> --
          >> Miha Markic [MVP C#] - RightHand .NET consulting & development
          >> miha at rthand com
          >> www.rthand.com
          >>
          >> "Mojtaba Faridzad" <mfaridzad@hotm ail.com> wrote in message
          >> news:Oc$hnz1jEH A.3712@TK2MSFTN GP15.phx.gbl...[color=darkred]
          >> > Hi,
          >> >
          >> > (newbie in C#) is it a good idea to use SQL Connection, SQL Data
          >> > Adapter,... in C#? when I am design a form, I can use SQL Connection
          >> > and
          >> > set the properties to connect to a database. I guess after unloading
          >> > the
          >> > form, this connection will be closed. but as I know, it's better to
          >> > open[/color][/color]
          > a[color=green][color=darkred]
          >> > connection when we need it and after using, we should close it. I can
          >> > do
          >> > it without using SQL Connection component (by using SqlConnection[/color][/color]
          > class).[color=green][color=darkred]
          >> > now which one is better? using SQL Connection is very easy but I'd like[/color][/color]
          > to[color=green][color=darkred]
          >> > have the best performance.
          >> >
          >> > thanks,
          >> > Mojtaba
          >> >[/color]
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Mojtaba Faridzad

            #6
            Re: is it a good idea to use SQL connection?

            thanks Miha. I forgot to mention it on my previous post :)

            just I wanted to let you know that I am using MySQLDirect connection (not
            SqlConnection) but both they have the same components.

            Mojtaba

            "Mark Broadbent" <no-spam-please@no-spam-please.com> wrote in message
            news:e2$31J2jEH A.1644@tk2msftn gp13.phx.gbl...[color=blue]
            > also bear in mind Mojtaba that the SqlAdapter will dynamically open and
            > close the connection for you, so you should always let it do so (as
            > opposed
            > to manually opening and closing the connection).
            >
            > "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
            > news:OtI54%231j EHA.1644@tk2msf tngp13.phx.gbl. ..[color=green]
            >> Hi Mojtaba,
            >>
            >> In designer you only design the connection - it won't open it for you
            >> thus
            >> it won't make much difference.
            >> Though I preffer to create it by code right before I need it and dispose[/color]
            > it[color=green]
            >> as soon as possible.
            >>
            >> --
            >> Miha Markic [MVP C#] - RightHand .NET consulting & development
            >> miha at rthand com
            >> www.rthand.com
            >>
            >> "Mojtaba Faridzad" <mfaridzad@hotm ail.com> wrote in message
            >> news:Oc$hnz1jEH A.3712@TK2MSFTN GP15.phx.gbl...[color=darkred]
            >> > Hi,
            >> >
            >> > (newbie in C#) is it a good idea to use SQL Connection, SQL Data
            >> > Adapter,... in C#? when I am design a form, I can use SQL Connection
            >> > and
            >> > set the properties to connect to a database. I guess after unloading
            >> > the
            >> > form, this connection will be closed. but as I know, it's better to
            >> > open[/color][/color]
            > a[color=green][color=darkred]
            >> > connection when we need it and after using, we should close it. I can
            >> > do
            >> > it without using SQL Connection component (by using SqlConnection[/color][/color]
            > class).[color=green][color=darkred]
            >> > now which one is better? using SQL Connection is very easy but I'd like[/color][/color]
            > to[color=green][color=darkred]
            >> > have the best performance.
            >> >
            >> > thanks,
            >> > Mojtaba
            >> >[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            Working...