Having a connection class that only does connection work

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

    #1

    Having a connection class that only does connection work

    Is it possible to have a Connection class that opens up connection to my DB
    and leaves it open until I decide to close it?
    I run many SQL statements so I was wondering if it's possible to call a
    Class1.GetProdu cts that sets up an SQL statement and then calls another
    Class2.Connect that actually opens up the connection Class2.Connect would
    not receive anything (since the connections string is always the same) and
    would not return anything.

    Thanks.


  • William Ryan

    #2
    Re: Having a connection class that only does connection work

    > Is it possible to have a Connection class that opens up connection to my
    DB[color=blue]
    > and leaves it open until I decide to close it?[/color]

    In many instances, if you are using any of the 'connected' ado.net objects,
    you don't have a choice, you have to open them manually and close them when
    you are done which is determined by you.
    [color=blue]
    > I run many SQL statements so I was wondering if it's possible to call a
    > Class1.GetProdu cts that sets up an SQL statement and then calls another
    > Class2.Connect that actually opens up the connection Class2.Connect would
    > not receive anything (since the connections string is always the same) and
    > would not return anything.[/color]

    Yes, you can do that, but if I understand you correctly, I don't understand
    what you are getting by it. You can define a static property connection
    somewhere and instantiate it in your class locally each time you need it.
    Leaving a connection open just for leaving it's open sake is kind of
    antithetical to everything ADO.NET, and unless you are firing A LOT of
    sequential statements, I don't know that there's much value if the
    connection string remains identical and everything is sitting tin the pool.

    If you could describe your ultimate goal, I could probably be of more
    assistance, but for the time being, I think theanswer is Yes to both of your
    questions.

    Cheers,

    Bill
    "Jim" <NA> wrote in message news:uIQPEbbeDH A.3096@TK2MSFTN GP11.phx.gbl...
    [color=blue]
    >
    > Thanks.
    >
    >[/color]


    Comment

    • Jim

      #3
      Re: Having a connection class that only does connection work

      I want my code to be as easy to maintain as possible. Basically, to get any
      records from my DB, I use two classes: one that sets up the SQL statement,
      and another that makes the connection (plus the class that contains the
      windows form). Now, is this the best way to divide my tasks? And how can I
      update the dataset in frm_AddNewProdu ct?

      This is how it looks like:
      public class frm_AddNewProdu ct : System.Windows. Forms.Form

      private void frm_AddNewProdu ct_Load(object sender, System.EventArg s e)

      { ...

      DataAccess DataAccess = new DataAccess();

      DS_Products = DataAccess.GetP roducts();

      .... }

      ----------------------------

      public class DataAccess

      { ....

      public DataSet GetProducts()
      {
      string mySql = "Select itemid, Description, Quantity, Price from
      products where producttype = 'P'";
      DBConnection myConn = new DBConnection("S QLServer", mySql);
      DataSet DS_MyProducts = new DataSet();
      DS_MyProducts = myConn.Connecti on();
      return DS_MyProducts;
      }
      }
      ----------------
      public class DBConnection
      {
      public DataSet MyClientsDS;
      public SqlDataAdapter MyDataAdapterCl ients;
      string strThisConn, strThisQuery, strTable;
      public DBConnection(st ring strDataBase, string strQuery)
      {
      strThisConn = strDataBase;
      strThisQuery = strQuery;
      strTable = strMainTable;
      }
      public DataSet Connection()
      {
      MyClientsDS = new DataSet();
      SqlConnection SQLConn = new SqlConnection(" Data Source=localhos t;
      Integrated Security=SSPI;" +
      "Initial Catalog=invoici ng");
      MyDataAdapterCl ients = new SqlDataAdapter (strThisQuery, SQLConn);
      MyDataAdapterCl ients.Fill(MyCl ientsDS);
      SQLConn.Close() ;
      return MyClientsDS;
      }
      }

      Thanks for your help.


      "William Ryan" <dotnetguru@nos pam.comcast.net > wrote in message
      news:efOtYhieDH A.2140@TK2MSFTN GP09.phx.gbl...[color=blue][color=green]
      > > Is it possible to have a Connection class that opens up connection to my[/color]
      > DB[color=green]
      > > and leaves it open until I decide to close it?[/color]
      >
      > In many instances, if you are using any of the 'connected' ado.net[/color]
      objects,[color=blue]
      > you don't have a choice, you have to open them manually and close them[/color]
      when[color=blue]
      > you are done which is determined by you.
      >[color=green]
      > > I run many SQL statements so I was wondering if it's possible to call a
      > > Class1.GetProdu cts that sets up an SQL statement and then calls another
      > > Class2.Connect that actually opens up the connection Class2.Connect[/color][/color]
      would[color=blue][color=green]
      > > not receive anything (since the connections string is always the same)[/color][/color]
      and[color=blue][color=green]
      > > would not return anything.[/color]
      >
      > Yes, you can do that, but if I understand you correctly, I don't[/color]
      understand[color=blue]
      > what you are getting by it. You can define a static property connection
      > somewhere and instantiate it in your class locally each time you need it.
      > Leaving a connection open just for leaving it's open sake is kind of
      > antithetical to everything ADO.NET, and unless you are firing A LOT of
      > sequential statements, I don't know that there's much value if the
      > connection string remains identical and everything is sitting tin the[/color]
      pool.[color=blue]
      >
      > If you could describe your ultimate goal, I could probably be of more
      > assistance, but for the time being, I think theanswer is Yes to both of[/color]
      your[color=blue]
      > questions.
      >
      > Cheers,
      >
      > Bill
      > "Jim" <NA> wrote in message news:uIQPEbbeDH A.3096@TK2MSFTN GP11.phx.gbl...
      >[color=green]
      > >
      > > Thanks.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...