better approaches

V

Vicky

which one is better approach among the two listed below

1. Opening connection on each web page for doing some database transaction

2. Opening connection once and store it in application object, and then
refering this application object on each web page.

Thanks
 
N

.NET Follower

dear vicky
both r wrong
store ur connectn string always in the web.config files
as
<appSettings>

<add key="anyname" value="Data source=xyz;initial catalog=DBNAME;user
ed=sa;password=sa"/>

<add key="TimeOut" value="10"/>

</appSettings>
 
V

Vicky

I think u misinterpreted by question, i mean not connection string but
instance of open connection object connected to a Sql server database

thanks
 
M

Munsifali Rashid

I think generally, it's better to open the connection, do your database
stuff, and then close the connection on each page. As far as I know, the
connnection pooling and optimisation stuff is done by the .net framework and
windows.

Mun
 
R

Rocky Moore

Option (3) ;)

You should use a connection and then close it when you no longer need it. If
you have a group of calls you want to make, you can open the connection and
live it open until the rest of the steps are complete (as long as there is
no collision on the connection), but it is best if this time frame is short,
you would not want to leave connections open past the duration of the page
request. In this way you allow the ADO.NET handling of connection pooling
to function.

If you are using datasets, simply allow the adapter to open and close the
connections as required.

As a side note, make sure you close all your datareaders when you are
finished with them (if you use them at all).
 
N

.NET Follower

ya
i misinterprated ur ?

it is always a better practise to open and close ur DB connectn
immediately on every page.
imagine numerous clients opening a connection to DB
so how much resources is blocked
and so ur app will definately crash.

some objects like the dataadapter does itself this job

however for Action queries like ExecuteNONQuery etc
u hv to xplicitly open and close connectn s

so 1 is recommended.
 
V

Vicky

Thanks, i too was doing this only.

Munsifali Rashid said:
I think generally, it's better to open the connection, do your database
stuff, and then close the connection on each page. As far as I know, the
connnection pooling and optimisation stuff is done by the .net framework and
windows.

Mun
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top