Database update with auto connection open/close

Discussion in 'ASP .Net' started by Maziar Aflatoun, Aug 11, 2005.

  1. Hi,

    To update the database I first open a connection, update and then I close
    it. Is there way to do this without opening the connection first? Such as
    using the DataAdaptor (which manages the connection automatically)? Is that
    efficient?

    Thanks
    Maz.
    Maziar Aflatoun, Aug 11, 2005
    #1
    1. Advertising

  2. Maziar Aflatoun

    Brock Allen Guest

    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter("select * from authors", "server=server;database=pubs;trusted_connection==yes;");
    da.Fill(ds);

    This opens and closes the connection for you. Is it better? It depens what
    you do with the data and how long the DataSet lives.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Hi,
    >
    > To update the database I first open a connection, update and then I
    > close it. Is there way to do this without opening the connection
    > first? Such as using the DataAdaptor (which manages the connection
    > automatically)? Is that efficient?
    >
    > Thanks
    > Maz.
    Brock Allen, Aug 11, 2005
    #2
    1. Advertising

  3. Maziar Aflatoun

    tom pester Guest

    As you say, the data-adaptor just does this in the background. There is no
    way to prevent the opining of a connection of course so open it quick and
    close it asap.
    But you can use a library to make some things easier. Check out this article
    on the Data Access Application Block :

    http://aspnet.4guysfromrolla.com/articles/070203-1.aspx

    Here is an extract :

    =====
    string strSql = "select * from products where categoryid = 1";
    string strConnTxt = "Server=(local);Database=Northwind;Integrated Security=True;";

    DataGrid4.DataSource = SqlHelper.ExecuteReader(strConnTxt, CommandType.Text,
    strSql);
    DataGrid4.DataBind();
    =====

    It may be strange that you dont see a connection closed somewhere and it
    should ring an alarmbell.
    What happens is that the datareader gets its date from the command object
    with CommandBehavior.CloseConnection option so that the connection gets closed
    as soon as the datareader is closed.
    And the DataGrid4.DataBind() does close the datareader thus after that statement
    the connection is closed.

    It's bad practice I think to rely on such side effects just to let you know.


    Cheers,
    Tom Pester

    > Hi,
    >
    > To update the database I first open a connection, update and then I
    > close it. Is there way to do this without opening the connection
    > first? Such as using the DataAdaptor (which manages the connection
    > automatically)? Is that efficient?
    >
    > Thanks
    > Maz
    tom pester, Aug 11, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael
    Replies:
    2
    Views:
    4,299
    Kevin Spencer
    Dec 30, 2003
  2. Paul van Rossem
    Replies:
    0
    Views:
    593
    Paul van Rossem
    Apr 7, 2005
  3. linkswanted
    Replies:
    1
    Views:
    877
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    820
    Iñaki Baz Castillo
    Jan 12, 2010
  5. Iulian Ilea
    Replies:
    1
    Views:
    291
    pcx99
    Dec 21, 2006
Loading...

Share This Page