编程 - 最佳实践 [英] Programming - Best Practice

查看:74
本文介绍了编程 - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


过去几个月我一直在教自己.NET并且已经取得了一些成功。我想问一个问题...


我所遵循的一些例子在他们的

最后声明中有以下内容


尝试

......

Catch

......

最后


如果(Not IsNothing(dbConn))那么

dbConn.Close()

结束如果


结束尝试


我的数据连接是否总是使用此方法关闭,或者我应该使用dbConn.close来支持



我有一个似乎保持连接的程序 - 我是否b / b
缺少某些东西?

解决方案



" bigHairy" < mt ********* @ gmail.comwrote in message

news:11 ********************* *@m58g2000cwm.googlegr oups.com ...


你好。


我一直在教自己.NET最近几个月已经取得了一些成功。我想问一个问题...


我所遵循的一些例子在他们的

最后声明中有以下内容


尝试

.....

Catch

.....

最后


如果(Not IsNothing(dbConn))那么

dbConn.Close()

结束如果


结束尝试


我的数据连接是否总是使用这种方法关闭,或者我应该使用dbConn.close来支持
?br />

我有一个似乎保持连接的程序 - 我是否b / b
缺少某些东西?



如果您只使用dbConn.Close(),如果

问题中的连接未打开,您将收到异常。

最后一次检查中的代码看看dbConn是否是一个空引用(没有

活动连接),只有尝试关闭它才有一个活动的

连接要关闭。

由于finally块中的代码是guarantee ed执行,你发布的代码

关闭连接*如果*它是打开的。

问题是,是什么让你认为连接后仍然有效/>
此代码执行?


感谢您的及时回复,感谢您的帮助。


我是目前正在与第三方数据提供商进行讨论,因为使用带有Progress

数据库的链接SQL Server似乎是超时问题。有人建议可能是我们的程序打开了

连接打开但是我找不到这个证据 - 我想我会

检查我是否使用最佳实践来关闭这些连接

作为我调查剩余的

流程之前的第一个停靠点。


你可以使用这样的东西来关闭你的连接:


如果objConn.State = Data.ConnectionState.Open那么

objConn.Close()

结束如果



" bigHairy" < mt ********* @ gmail.comwrote in message

news:11 ********************* *@m58g2000cwm.googlegr oups.com ...


你好。


我一直在教自己.NET最近几个月已经取得了一些成功。我想问一个问题...


我所遵循的一些例子在他们的

最后声明中有以下内容


尝试

.....

Catch

.....

最后


如果(Not IsNothing(dbConn))那么

dbConn.Close()

结束如果


结束尝试


我的数据连接是否总是使用这种方法关闭,或者我应该使用dbConn.close来支持
?br />

我有一个似乎保持连接的程序 - 我是否b / b
缺少一些东西?



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(dbConn)) 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?

解决方案


"bigHairy" <mt*********@gmail.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...

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(dbConn)) 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?

If you just use dbConn.Close(), you''ll get an exception if the connection in
question is not open.
The code in finally first checks to see if dbConn is an empty reference (no
active connection), and only attempts to close it is there is an active
connection to close.
As code in the finally block is "guaranteed to execute", the code you posted
closes the connection *if* it is open.
The question is, what makes you think the connection remains active after
this code executes?


Thanks for your prompt reply, I appreciate the help.

I am currently in discussions with a 3rd party data provider as there
seem to be timeout issues using a linked SQL Server with a Progress
Database. There are suggestions that it may be our program leaving
connections open but I can find no evidence of this - thought I would
check that I am using the best practice for closing these connections
as a first port of call before I go investigating the rest of the
process.


You could use something like this to close your connection:

If objConn.State = Data.ConnectionState.Open Then
objConn.Close()
End If


"bigHairy" <mt*********@gmail.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...

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(dbConn)) 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?



这篇关于编程 - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆