Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Database update with auto connection open/close (http://www.velocityreviews.com/forums/t107897-database-update-with-auto-connection-open-close.html)

Maziar Aflatoun 08-11-2005 01:20 PM

Database update with auto connection open/close
 
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 08-11-2005 02:48 PM

Re: Database update with auto connection open/close
 
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from authors", "server=server;database=pubs;trusted_connection==y es;");
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.





tom pester 08-11-2005 02:51 PM

Re: Database update with auto connection open/close
 

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





All times are GMT. The time now is 08:03 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57