PC Review


Reply
Thread Tools Rate Thread

Database update with auto connection open/close

 
 
Maziar Aflatoun
Guest
Posts: n/a
 
      11th Aug 2005
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.


 
Reply With Quote
 
 
 
 
Brock Allen
Guest
Posts: n/a
 
      11th Aug 2005
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.




 
Reply With Quote
 
tom pester
Guest
Posts: n/a
 
      11th Aug 2005

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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
open and close database connection in classes Imran Aziz Microsoft C# .NET 8 15th Aug 2006 04:53 PM
Singleton Pattern for Database Access --- Leave Open or Close Connection mescano Microsoft VB .NET 4 13th Apr 2005 03:12 PM
Auto close a database =?Utf-8?B?Q0s=?= Microsoft Access Form Coding 1 27th Nov 2004 10:22 PM
Open/Close Database Connection for each page Michael Microsoft ASP .NET 2 30th Dec 2003 07:34 PM
Re: Auto close the connection ??? Kelly Windows XP General 0 29th Jul 2003 11:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:02 PM.