Re: How to: Centralize Connection Objects

From: charliewest (charliewest_at_discussions.microsoft.com)
Date: 10/27/04


Date: Wed, 27 Oct 2004 08:01:06 -0700

Is there a significant performance hit in declaring the connection and
command objects over and over again? Every example i find declare these
objects as class level variables, versus procedural vars. Thanks.

"Nicholas Paldino [.NET/C# MVP]" wrote:

> Charlie,
>
> This is exactly what I was thinking, which is why I made the last
> comment:
>
> I would also say that you should have a method that will generate your
> connection for you, that way you can change
> the way that the connection information is obtained (registry, file, etc,
> etc), and it makes it available elsewhere (instead of you having to perform
> the logic everywhere).
>
> The implication here (which is not obvious, I am sorry) is that you can
> get the connection from anywhere, if you need to, and then use it. So you
> could do something like this:
>
> // Get the connection.
> using (SqlCeConnection conn = MyLib.GetConnection())
> {
> // Open the connection.
> conn.Open();
>
> // Use it multiple times.
> }
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@spam.guard.caspershouse.com
>
> "charliewest" <charliewest@discussions.microsoft.com> wrote in message
> news:F2EBACA8-BDFC-4824-825F-6FA2B5D4FD22@microsoft.com...
> > Thanks Nicholas. The example you've provided seems ideal for executing
> > *individual* or standalone commands.
> >
> > In my program, however, procedures execute several commands using the same
> > connection. (My sample code was a little too generic - sorry). My guess is
> > that the sample you provided would not be ideal for this scenario as
> > creating
> > a new connection and command object so many times in the same procedure
> > must
> > affect performance on a pocket pc?
> >
> > When i originally asked this question (although i didn't specify this) i
> > was
> > thinking along the lines of encapsulating the connection object and then
> > returning the connection object to the calling procedure, then executing
> > my
> > commands, and then close the connection object somehow. But i haven't seen
> > this done anywhere.
> >
> > Can you let me know if i am on the right track? Or, if what i am thinking
> > isn't possible/recommended. Thanks.
> >
> >
> >
> > "Nicholas Paldino [.NET/C# MVP]" wrote:
> >
> >> Charlie,
> >>
> >> You could do something like this:
> >>
> >> public static void ExecuteNonQuery(string sql)
> >> {
> >> // Open a connection.
> >> using (SqlCeConnection cn = new SqlCeConnection(@"file"))
> >> {
> >> // Open the connection.
> >> cn.Open();
> >>
> >> // Create a command.
> >> using (SqlCeCommand cmd = new SqlCeCommand(sql, cn))
> >> {
> >> // Execute.
> >> cmd.ExecuteNonQuery();
> >> }
> >> }
> >> }
> >>
> >> And that should work fine. I would also say that you should have a
> >> method that will generate your connection for you, that way you can
> >> change
> >> the way that the connection information is obtained (registry, file, etc,
> >> etc), and it makes it available elsewhere (instead of you having to
> >> perform
> >> the logic everywhere).
> >>
> >> Hope this helps.
> >>
> >>
> >> --
> >> - Nicholas Paldino [.NET/C# MVP]
> >> - mvp@spam.guard.caspershouse.com
> >>
> >>
> >>
> >>
> >> "charliewest" <charliewest@discussions.microsoft.com> wrote in message
> >> news:7AAF97B4-58D9-46E5-B4FA-33664C11465F@microsoft.com...
> >> >I have developed an application for WM 2003, which frequently transacts
> >> >with
> >> > a sql server ce 2.0 database. I have several procedures which utilize
> >> > the
> >> > following code:
> >> >
> >> > cn = new SqlCeConnection(@"<< sdf file >>");
> >> > cn.Open();
> >> > cmd = new SqlCeCommand(<< sql string >>, cn);
> >> > cmd.ExecuteNonQuery();
> >> > cn.Close()
> >> >
> >> > My question is: How can i best encapsulate this as it is repeated
> >> > several
> >> > times in my program?
> >> >
> >> > All of the examples i find declare both connection objects and command
> >> > objects in *each* procedure, but i can only assume that this is not
> >> > efficient?
> >> >
> >> > I have made the "cn" connection object and "cmd" command object private
> >> > to
> >> > the entire class in hopes to eliminate the number of objects which are
> >> > declared.
> >> >
> >> > Any other suggestions? Thanks.
> >> >
> >> >
> >>
> >>
> >>
>
>
>



Relevant Pages

  • Re: why not SQL Authentication?
    ... > the Connection String. ... > step is caching your command objects. ... Another way to avoid latency is caching data returned from the database (typically ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: releasing memory...
    ... > command objects. ... > I have a procedure to populate dataset.I call dispose on all ... > objects after dataset is populate and close the connection. ...
    (microsoft.public.dotnet.languages.vb)
  • releasing memory...
    ... command objects. ... I have a procedure to populate dataset.I call dispose on all objects ... I see that the application still uses connection on sqlserver(I ...
    (microsoft.public.dotnet.languages.vb)
  • memory..
    ... I wrote a ado.net application that uses dataadapter,connection and command objects. ... I have a procedure to populate dataset.I call dispose on all objects after dataset is populate and close the connection. ...
    (microsoft.public.dotnet.framework.adonet)
  • releasing memory ...
    ... command objects. ... I have a procedure to populate dataset.I call dispose on all objects ... I see that the application still uses connection on sqlserver(I ...
    (microsoft.public.dotnet.framework.adonet)