473,769 Members | 5,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programming - Best Practice

Hello.

I have been teaching myself .NET over the last few months and have had
some success. I would like to ask a question though...

A number of examples I have followed have the following in their
finally statement

Try
......
Catch
......
Finally

If (Not IsNothing(dbCon n)) Then
dbConn.Close()
End If

End Try

Will my data connection always close using this method or should I
just use dbConn.close?

I have a program that seems to be holding onto its connection - am I
missing something?

Feb 16 '07
51 3662
"Cor Ligthert [MVP]" <no************ @planet.nlschri eb:
>Not really. Imagine an exception gets thrown when opening the
connection.
Absolutely not Herfried that is in your control, I did not say you should
not use Try and Catch. I mean there should not be needed a question as "If
connection is open"
Why not? Pseudo code:

\\\
Dim c As New Connection()
Try
c.Open()
c.DoSomething()
Catch
...
Finally
c.Close()
End Try
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Feb 17 '07 #21
Herfried,

In your sample you don't know if it is an opening error or a processing
error.

It depend of course of the situation, I think that in a common situation (I
don't remember me that I did the first one in a real situation) that I am to
lazy for the first one, however asked was Best Practice.

Your sample is often used to show how finally can work. In my opinion that
is not because it is the best way how to do an open and close but there are
not so many more simple samples.

:-)

Cor

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.atschre ef in bericht
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
"Cor Ligthert [MVP]" <no************ @planet.nlschri eb:
>>Not really. Imagine an exception gets thrown when opening the
connection.
Absolutely not Herfried that is in your control, I did not say you should
not use Try and Catch. I mean there should not be needed a question as
"If connection is open"

Why not? Pseudo code:

\\\
Dim c As New Connection()
Try
c.Open()
c.DoSomething()
Catch
...
Finally
c.Close()
End Try
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Feb 17 '07 #22
pf******@hotmai l.com wrote in news:1171659636 .108644.239340
@q2g2000cwa.goo glegroups.com:
I just don't understand; why do you even need to close a connection if
you can't leave a connection OPEN?
It depends on the reader you use. If you use a data adapter, it'll close
for you.

However, datareaders use a firehose mode to read from the database, thus
you need to manually close it when you're done with the reader/connection.
Feb 17 '07 #23
That is not correct.

When a DataAdapter 'starts' it's operation it determines thae current state
of the Connection. If the Connection was already open then it leaves it open
when it finishes. If it needs to open the Connection then it closes the
Connection when it finishes.

In other words, it leaves the Connection in the state that it found it.
"Spam Catcher" <sp**********@r ogers.comwrote in message
news:Xn******** *************** **********@127. 0.0.1...
pf******@hotmai l.com wrote in news:1171659636 .108644.239340
@q2g2000cwa.goo glegroups.com:
>I just don't understand; why do you even need to close a connection if
you can't leave a connection OPEN?

It depends on the reader you use. If you use a data adapter, it'll close
for you.

However, datareaders use a firehose mode to read from the database, thus
you need to manually close it when you're done with the reader/connection.
Feb 17 '07 #24
Spam

that is not an acceptable answer.

Your ADO.net _CRAP_ is unusable

UNNECESSARY CHANGE IS NOT SEXY
UNNECESSARY CHANGE IS NOT WANTED
UNNECESSARY CHANGE IS NOT ACCEPTABLE


On Feb 17, 12:31 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
pfc_s...@hotmai l.com wrote in news:1171659636 .108644.239340
@q2g2000cwa.goo glegroups.com:
I just don't understand; why do you even need to close a connection if
you can't leave a connection OPEN?

It depends on the reader you use. If you use a data adapter, it'll close
for you.

However, datareaders use a firehose mode to read from the database, thus
you need to manually close it when you're done with the reader/connection.

Feb 19 '07 #25
Stephany

I don't care whether is 'says' it is open or not.
it does not maintain the same spid; you and Microsoft merely play word
games.

ADO.net is not usable, UNNECESSARY CHANGE IS NOT ACCEPTABLE


On Feb 17, 12:38 pm, "Stephany Young" <noone@localhos twrote:
That is not correct.

When a DataAdapter 'starts' it's operation it determines thae current state
of the Connection. If the Connection was already open then it leaves it open
when it finishes. If it needs to open the Connection then it closes the
Connection when it finishes.

In other words, it leaves the Connection in the state that it found it.

"Spam Catcher" <spamhoney...@r ogers.comwrote in message

news:Xn******** *************** **********@127. 0.0.1...
pfc_s...@hotmai l.com wrote in news:1171659636 .108644.239340
@q2g2000cwa.goo glegroups.com:
I just don't understand; why do you even need to close a connection if
you can't leave a connection OPEN?
It depends on the reader you use. If you use a data adapter, it'll close
for you.
However, datareaders use a firehose mode to read from the database, thus
you need to manually close it when you're done with the reader/connection.- Hide quoted text -

- Show quoted text -

Feb 19 '07 #26
Spam

ok.. ok.. how we're getting somewhere

if I just use datareaders (why in the hell would I want to use
datasets?)
then it will keep the same SPID even with 20 people hitting the
database?

I just think that the whole 'automagically close connections' is
misguided
and when MS Press sits there and says crap like 'uh you guys cant
remember to use the .MoveNext method so we've fixed it for you by
using the .Read' method

I just get a little bit defensive

I didn't ask for MIcrosoft to talk down to us-- just because we use
the WORLDS MOST POPULAR PROGRAMMING LANGUAGE
I mean; are you guys not pissed off by the tone of that authors voice?

it's just reflective of what M$ thinks of VB people

so... _FUCK_MS_


On Feb 17, 12:31 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
pfc_s...@hotmai l.com wrote in news:1171659636 .108644.239340
@q2g2000cwa.goo glegroups.com:
I just don't understand; why do you even need to close a connection if
you can't leave a connection OPEN?

It depends on the reader you use. If you use a data adapter, it'll close
for you.

However, datareaders use a firehose mode to read from the database, thus
you need to manually close it when you're done with the reader/connection.

Feb 19 '07 #27
I don't WANT to manually close it.
I want to leave it OPEN.
I don't see the point of opening it and re-opening it and all that
crap
and you can't utilize temp tables if your provider keeps on randomly
closing connections

uh; like seriously here

let's talk about programming apps for small business.
why would I constantly open and close and open and close connections??

On Feb 17, 12:31 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
pfc_s...@hotmai l.com wrote in news:1171659636 .108644.239340
@q2g2000cwa.goo glegroups.com:
I just don't understand; why do you even need to close a connection if
you can't leave a connection OPEN?

It depends on the reader you use. If you use a data adapter, it'll close
for you.

However, datareaders use a firehose mode to read from the database, thus
you need to manually close it when you're done with the reader/connection.

Feb 19 '07 #28
PFC,

I have the same, when I have to drive less than 150miles an hour, than I
hate that, why would I do that, just for security by instance like opening
and closing the connections to prevent that there are pooling of whatever
problems.

Cor
"PFC Sadr" <pf******@hotma il.comschreef in bericht
news:11******** **************@ l53g2000cwa.goo glegroups.com.. .
>I don't WANT to manually close it.
I want to leave it OPEN.
I don't see the point of opening it and re-opening it and all that
crap
and you can't utilize temp tables if your provider keeps on randomly
closing connections

uh; like seriously here

let's talk about programming apps for small business.
why would I constantly open and close and open and close connections??

On Feb 17, 12:31 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
>pfc_s...@hotma il.com wrote in news:1171659636 .108644.239340
@q2g2000cwa.go oglegroups.com:
I just don't understand; why do you even need to close a connection if
you can't leave a connection OPEN?

It depends on the reader you use. If you use a data adapter, it'll close
for you.

However, datareaders use a firehose mode to read from the database, thus
you need to manually close it when you're done with the
reader/connection.


Feb 20 '07 #29
Cor

I don't understand what you're trying to say to me here.

Are you saying that you are GLAD that they automagically close
connections?

IT SHOULD BE OPTIONAL NOT MANDATORY

JUST LIKE IT WAS IN ADO; USING CONNECTION SHARING


On Feb 19, 8:32 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
wrote:
PFC,

I have the same, when I have to drive less than 150miles an hour, than I
hate that, why would I do that, just for security by instance like opening
and closing the connections to prevent that there are pooling of whatever
problems.

Cor

"PFC Sadr" <pfc_s...@hotma il.comschreef in berichtnews:11* *************** ******@l53g2000 cwa.googlegroup s.com...
I don't WANT to manually close it.
I want to leave it OPEN.
I don't see the point of opening it and re-opening it and all that
crap
and you can't utilize temp tables if your provider keeps on randomly
closing connections
uh; like seriously here
let's talk about programming apps for small business.
why would I constantly open and close and open and close connections??
On Feb 17, 12:31 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
pfc_s...@hotmai l.com wrote in news:1171659636 .108644.239340
@q2g2000cwa.goo glegroups.com:
I just don't understand; why do you even need to close a connection if
you can't leave a connection OPEN?
It depends on the reader you use. If you use a data adapter, it'll close
for you.
However, datareaders use a firehose mode to read from the database, thus
you need to manually close it when you're done with the
reader/connection.- Hide quoted text -

- Show quoted text -

Feb 20 '07 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

136
9447
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
12
8480
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now I really want to get back into it as I see a point with GNU/Linux. I want to get my old skills back and write something or help on some projects etc. I need some good books. I used to have one called "A Book On C", but sold it,
8
2387
by: Dale | last post by:
I've searched Amazon and read probably 100 reviews but can't find what seems to be any book that is widely accepted as the definitive book on object oriented programming design and techniques. And most of the highest rated are all written 10 to 15 years ago. Any good suggestions?
19
2303
by: JoeC | last post by:
I have seen many books that teack coding for C++. What are ways to improve my techniques for writing larger programs. I have written many demo programs learning some aspects of code wether it be dynamic binding or creating function and I understand most of that but how can I learn and employ techniques to make my programs better. I try to employ the lessons I learn in the programs that I write. Acclerated C++ is the best book I have...
3
1480
by: Ray | last post by:
OK, maybe I shoot a more general question to the group since there are so many great programmers here: how do you practice your craft? I do it in the following way: 1. Set aside 30 minutes to 1 hour a day to read up on the latest development, be it about the tool I'm using, the language, or the platform, or the framework, etc. 2. Once every 1-2 months, go to Amazon, and look for the book with the
7
1509
by: Michael | last post by:
Hey, I'm, I guess, an itermediate programmer and I have a question about learning any programming language. I understand that as a programmer you're going to probably constantly be re-writing code and the best method would be to save a template or other etc. I also know that it's ok to use other people's code to help you do something, but wouldn't it be best to re-type the code or study it until YOU learn how to do it and you actually...
7
4963
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already developed significant content for the C programming language that is available at: https://www.securecoding.cert.org/ by clicking on the "CERT C Programming Language Secure Coding Standard"
6
1344
by: Michael | last post by:
Hi, A quick and most likely simply question about which is generally better programming with PHP. Is it more proper to break out of PHP code to write HTML, or is it ok rto do it within print() statements? Such as... SCRENARIO A) <?php ....code... ?>
9
1901
by: Smithers | last post by:
Please consider this humble method: public void ResetCounters() { m_TotalExceptionsDetected = 0; m_TotalMessagesSent = 0; } Given no further information, would you wrap those two lines in a try... catch block?
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.