DB Connections in ASP.net ?

G

Guest

Good evening all,

Have a question about coonections. We are disscusing two ways;

1] Open the connection. Run your SQL. Close the connection.
This done is a function or sub.

Or

2] Open connection on page load/Init.
It stays open during life of page. When you move from page to another one or when browser is killed you
then close the connection.

Which is the best way?
Life of page? While it is shown on client Browser?
or
is life of page ended when it is completely loaded in client browser.
If so if you closed the connection in a finished page event would that be better then
opening a connection, doing the SQL cmd, disposing of the connection in a fucntion or procedure.

Hope this all made sense. Its just ever where I read it says open it, do it, then kill it.

Thanks
Deasun
 
C

Cor

Hi Daesun,

The answer is quiet simple

Open your connection, (When you use a dataadapter that does that for you)
Get your data
Close your connection.
or
Open your connection, (When you use a dataadapter that does that for you)
Update your data
Close your connection.

I hope this helps?

Cor
Have a question about coonections. We are disscusing two ways;

1] Open the connection. Run your SQL. Close the connection.
This done is a function or sub.

Or

2] Open connection on page load/Init.
It stays open during life of page. When you move from page to another
one or when browser is killed you
 
B

Bruno Alexandre

This done is a function or sub.

"function" is a function that will return something like:

function add( a, b )
add = a + b
end function

you can use <%=(add(2, 4))%>
it will display 6 in the browser

"sub" is a function that does NOT return anything

sub openDatabase( rs, sSQL, sConn )
set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = sConn
rs.Source = sSQL
rs.CursorType = 0
rs.CursorLocation = 3
rs.LockType = 3
rs.Open()
end sub

sub closeDatabase( rs )
rs.close()
set rs = nothing
end sub

you can use:
call openDatabase( rsTest, "select top 5 * from table", "dns=testConn;" )

<%
do while not rsTest.eof
response.write( rsTest.fields.item("[name of the field]").value &
"<br>" )
rsTest.moveNext()
loop
call closeDatabase(rsTest)
%>


hope it helps explain when to use a function and a sub
Note: this examples are for ASP not ASP.NET but it helps explain the
function/sub differences
--


Bruno Miguel Alexandre
Dep Informática do Grupo Filtrarte

Av General Humberto Delgado, 91
Vila Verde
2705-887 Terrugem SNT
Portugal

T. +351 219 608 130
F. +351 219 615 369
w. www.filtrarte.com
@. (e-mail address removed)

Deasun said:
Good evening all,

Have a question about coonections. We are disscusing two ways;

1] Open the connection. Run your SQL. Close the connection.
This done is a function or sub.

Or

2] Open connection on page load/Init.
It stays open during life of page. When you move from page to another
one or when browser is killed you
 
M

Mark Fitzpatrick

Opening and then closing the connections as soon as you are done is often
the best way. It helps ensure that you are using the smallest possible
window of resources to communicate with the DB. When talking about the life
of the page, the life is not what you think. The life of the page is only
the time it takes to process the page and send to the browser. It has
nothing to do with the time a user spends on a page. You also don't know
when a browser is killed or the user navigates to another page because the
internet is stateless. The only time the server knows about the user is when
they make a request.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

Deasun said:
Good evening all,

Have a question about coonections. We are disscusing two ways;

1] Open the connection. Run your SQL. Close the connection.
This done is a function or sub.

Or

2] Open connection on page load/Init.
It stays open during life of page. When you move from page to another
one or when browser is killed you
 

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,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top