better approaches

  • Thread starter Thread starter Vicky
  • Start date Start date
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
 
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>
 
I think u misinterpreted by question, i mean not connection string but
instance of open connection object connected to a Sql server database

thanks
 
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
 
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).
 
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.
 
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
 
Back
Top